WPF DataGrid SelectedCellsChanged geting"父DataGrid上SelectionUnit属性的当前值阻止选择行."

Dal*_*ler 6 c# wpf datagrid

请帮助我,我正在编写" WPF应用程序框架 "和EF Code First的应用程序.我正在尝试将选定行设置为ViewModels变量"SelectedRawMaterial",该变量绑定到DataGrids SelectedItem并引发异常:"父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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在这个地方,它引发了异常.

ViewModel.SelectedRawMaterial = rm;
Run Code Online (Sandbox Code Playgroud)

DataGrids代码

<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>
Run Code Online (Sandbox Code Playgroud)

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

Cra*_*nec 7

因为你有数据网格设置的SelectionUnit (参见属性的定义)属性,Cell我相信你一次尝试选择一行.

编辑:如果更改SelectionUnitCellOrRowHeader允许单元格选择,但绑定选择整行

  • CellOrRowHeader可以满足您的需求吗? (2认同)