【问题标题】:WPF DataGrid SelectedCellsChanged geting "The current value of the SelectionUnit property on the parent DataGrid prevents rows from being selected."WPF DataGrid Selected Cell Changed 获取“父 DataGrid 上的 SelectionUnit 属性的当前值阻止选择行。”
【发布时间】:2012-10-05 00:07:59
【问题描述】:

请帮助我,我正在使用“WPF Application Framework”和 EF Code First 编写应用程序。我正在尝试将选定的行设置为绑定到 DataGrids SelectedItem 的 ViewModels 变量“SelectedRawMaterial”,并引发异常:“父 DataGrid 上的 SelectionUnit 属性的当前值阻止选择行。”

private void rawMaterialTable_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        DataGridCell cell = null;
        try
        {
            cell = DataGridHelper.GetCell(rawMaterialTable.SelectedCells[0]);
        }
        catch (Exception)
        { }

        if (cell != null)
        {
            int i = DataGridHelper.GetRowIndex(cell);
            try
            {

                RawMaterial rm = (RawMaterial)rawMaterialTable.Items[i];
                ViewModel.SelectedRawMaterial = rm;
            }
            catch (Exception) { }
        }
    }



public static class DataGridHelper
{
    public static DataGridCell GetCell(DataGridCellInfo dataGridCellInfo)
    {
        if (!dataGridCellInfo.IsValid)
        {
            return null;
        }

        var cellContent = dataGridCellInfo.Column.GetCellContent(dataGridCellInfo.Item);
        if (cellContent != null)
        {
            return (DataGridCell)cellContent.Parent;
        }
        else
        {
            return null;
        }
    }

    public static int GetRowIndex(DataGridCell dataGridCell)
    {
        // Use reflection to get DataGridCell.RowDataItem property value.
        PropertyInfo rowDataItemProperty = dataGridCell.GetType().GetProperty("RowDataItem",
                                                                              BindingFlags.Instance |
                                                                              BindingFlags.NonPublic);

        DataGrid dataGrid = GetDataGridFromChild(dataGridCell);

        return dataGrid.Items.IndexOf(rowDataItemProperty.GetValue(dataGridCell, null));
    }

    public static DataGrid GetDataGridFromChild(DependencyObject dataGridPart)
    {
        if (VisualTreeHelper.GetParent(dataGridPart) == null)
        {
            throw new NullReferenceException("Control is null.");
        }
        if (VisualTreeHelper.GetParent(dataGridPart) is DataGrid)
        {
            return (DataGrid)VisualTreeHelper.GetParent(dataGridPart);
        }
        else
        {
            return GetDataGridFromChild(VisualTreeHelper.GetParent(dataGridPart));
        }
    }
}

在这个地方它会引发异常。

ViewModel.SelectedRawMaterial = rm;

数据网格代码

<DataGrid x:Name="rawMaterialTable" ItemsSource="{Binding RawMaterials}" SelectedItem="{Binding SelectedRawMaterial}" 
              CanUserDeleteRows="False" BorderThickness="0" SelectionMode="Single" SelectionUnit="Cell" IsReadOnly="false"
              Grid.Row="1" Grid.Column="1" Margin="1,1,1,1" PreviewKeyDown="rawMaterialTable_PreviewKeyDown" SelectedCellsChanged="rawMaterialTable_SelectedCellsChanged" >
            <DataGrid.InputBindings>
                <KeyBinding Command="{Binding RemoveCommand}" Key="Del"/>
                <KeyBinding Command="{Binding AddCommand}" Key="Insert"/>
                <KeyBinding Command="{Binding EditCommand}" Key="F3"/>
            </DataGrid.InputBindings>

            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Code, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, 
                                  ValidatesOnExceptions=True, NotifyOnValidationError=True}" 
                                Header="{x:Static p:Resources.Code}" Width="60" ElementStyle="{StaticResource TextCellElementStyle}"
                                EditingElementStyle="{StaticResource TextCellEditingStyle}" DisplayIndex="0"/>


            </DataGrid.Columns>
        </DataGrid>

我添加了 SelectionUnit="Cell" 因为我也想处理 CellKeyDown。

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    这是因为您将数据网格的SelectionUnit(see the definition of the property) 属性设置为Cell,我相信您正在尝试一次选择一行。

    已编辑:如果您将SelectionUnit 更改为CellOrRowHeader 以允许选择单元格但绑定选择整行

    【讨论】:

    • 我添加了 SelectionUnit="Cell" 因为我也想处理 CellKeyDown。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 2012-03-18
    • 2013-04-19
    • 2012-03-12
    • 2011-06-12
    • 2020-12-14
    • 2016-10-13
    相关资源
    最近更新 更多