c# - WPF DataGrid 选定的单元格更改为 "The current value of the SelectionUnit property on the parent DataGrid prevents rows from being selected."

标签 c# wpf datagrid

请帮助我,我正在使用“WPF Application Framework”和 EF Code First 编写应用程序。我正在尝试将选定行设置为绑定(bind)到 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。

最佳答案

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

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

关于c# - WPF DataGrid 选定的单元格更改为 "The current value of the SelectionUnit property on the parent DataGrid prevents rows from being selected.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12737747/

相关文章:

c# - 将观察者集合分配给列表框时出错

c# - 阻止对 log4net 中具有单独记录器的程序集进行日志记录

c# - Amazon 和 Evernote 的身份验证设置

WPF MVVM 和父子组合框

database - Silverlight 从数据库到数据网格

c# - WPF 数据网格行标题切换按钮视觉状态从代码隐藏更改

c# - 在 WPF (C#) 中的两个 Datagrid 之间拖放

c# - 用户是通过 UI 更改了该值,还是通过依赖属性更改了该值?

wpf - 如何从 ItemsControl 的 Item 后面的代码访问 ItemsControl 的 ItemsSource 元素?

.net - WPF 中的置于前台