【问题标题】:WPF DataGrid: How do I access a ComboBox in a specific row of a DataGridTemplateColumn?WPF DataGrid:如何访问 DataGridTemplateColumn 特定行中的 ComboBox?
【发布时间】:2013-12-13 20:30:57
【问题描述】:

我的应用使用以下 XAML 代码正确地使用 ComboBox 填充 DataGrid 列:

<DataGridTemplateColumn Header="Thickness">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding SteelThickness, RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem="{Binding BottomPlateThickness, UpdateSourceTrigger=PropertyChanged}" SelectionChanged="ComboBox_SelectionChanged" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

我正在尝试找出一个表达式,例如

ComboBox cmb = dataGrid[i].Column[11].ComboBox

一次检索一个 ComboBox。

TIA

【问题讨论】:

  • 你不应该像那样访问 DataTemplate 控件,你想做什么?因为很可能有更好的方法来实现您的目标
  • 我正在尝试调试我的代码,并检查使用 ComboBox 选择的项目是否实际上是 SelectedItem。
  • sa_ddam213: 既然你知识渊博,能不能请你看看我更大的问题?网址:(stackoverflow.com/questions/20249889/…)
  • 注意:XAML 代码取自这里:(social.technet.microsoft.com/wiki/contents/articles/…),示例编号 9。该演示运行良好。
  • 给它一个 x:Name 并从后面的代码中访问它。

标签: c# wpf xaml datagrid combobox


【解决方案1】:

您好,在完成您的问题后,我决定编写一个辅助类来按索引访问行和列。我想给出一个想法。我没有很好地测试它,所以可能会有一些问题。

完整解决方案accessing datagrid row and column through indices

//该类将帮助通过索引获取行、列或单元格。虽然可能没有对 null 和 indices 进行适当的检查 抱歉。我稍后会更新它。

    public static class DataGridExtension
{
    public static DataGridColumn GetColumnByIndices(this DataGrid dataGrid, int rowIndex, int columnIndex)
    {
        ValidateParameters(dataGrid, rowIndex, columnIndex);

        var row = dataGrid.GetRowByIndex(rowIndex);

        if (row != null)
            return row.GetRowColumnByIndex(columnIndex);

        return null;
    }

    public static DataGridCell GetCellByIndices(this DataGrid dataGrid, int rowIndex, int columnIndex)
    {
        ValidateParameters(dataGrid, rowIndex, columnIndex);

        var row = dataGrid.GetRowByIndex(rowIndex);

        if (row != null)
            return row.GetRowCellByColumnIndex(columnIndex);

        return null;
    }

    //TODO:Validate RowIndex
    public static DataGridRow GetRowByIndex(this DataGrid dataGrid, int rowIndex)
    {
        if (dataGrid == null)
            return null;

        return (DataGridRow)dataGrid.ItemContainerGenerator
                                                       .ContainerFromIndex(rowIndex);    
    }

    //TODO:Validate ColumnIndex
    public static DataGridColumn GetRowColumnByIndex(this DataGridRow row, int columnIndex)
    {
        if (row != null)
        {
            var cell=GetRowCellByColumnIndex(row, columnIndex);

            if(cell!=null)
                return cell.Column;
        }

        return null;
    }

    //TODO:Validate ColumnIndex
    public static DataGridCell GetRowCellByColumnIndex(this DataGridRow row, int columnIndex)
    {
        if (row != null)
        {
            DataGridCellsPresenter cellPresenter = row.GetVisualChild<DataGridCellsPresenter>();

            if (cellPresenter != null)
                return ((DataGridCell)cellPresenter.ItemContainerGenerator.ContainerFromIndex(columnIndex));
        }

        return null;
    }

    private static void ValidateParameters(DataGrid dataGrid,int rowIndex,int columnIndex)
    {
        if (dataGrid == null)
            throw new ArgumentNullException("datagrid is null");
        if (rowIndex >= dataGrid.Items.Count)
            throw new IndexOutOfRangeException("rowIndex out of Index");
        if (columnIndex >= dataGrid.Columns.Count)
            throw new IndexOutOfRangeException("columnIndex out of Index");
    }
}

****//这个类将帮助找到VisualChild ****

public static class VisualHelper
{
    public static T GetVisualChild<T>(this Visual parent) where T : Visual
    {
        T child = default(T);

        for (int index = 0; index < VisualTreeHelper.GetChildrenCount(parent); index++)
        {
            Visual visualChild = (Visual)VisualTreeHelper.GetChild(parent, index);
            child = visualChild as T;

            if (child == null)
                child = GetVisualChild<T>(visualChild);//Find Recursively

            if (child != null)
                break;
        }
        return child;
    }
}

现在您可以使用这些类来获取 Columns、Cells、Rows by Index like

        private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        //Now we can get Column like this.
        var column = dataGrid.GetColumnByIndices(1, 1);

        //As SO want to find the ComboBox within that Column 
        ComboBox comboBox;
        var cell = dataGrid.GetCellByIndices(1, 1); //DataGridColumn Does'nt Inherit Visual class so using GetCellByIndices

        if(cell!=null)
            comboBox = cell.GetVisualChild<ComboBox>(); //DataGridCell Inherit Visual so we can use our VisualHelper Method
    }

【讨论】:

    猜你喜欢
    • 2013-06-30
    • 2014-10-28
    • 2014-06-12
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多