WPF DataGrid SelectedCellsChanged geting "The current value of the SelectionUnit property in the parent DataGrid prevents row selection."

Please help me, I am writing an application using the WPF Application Framework "and EF Code First. I am trying to set the selected row to the ViewModels variable" SelectedRawMaterial ", which is bound to the DataGrids SelectedItem and throws an exception:" The current value of the SelectionUnit property in the parent DataGrid does not allow select strings. "

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)); } } } 

At this point, it throws an exception.

 ViewModel.SelectedRawMaterial = rm; 

DataGrids Code

 <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> 

I added SelectionUnit = "Cell" because I also want to process CellKeyDown.

+4
source share
1 answer

Because you have the SelectionUnit property (see the definition of the property) of the property of the data grid installed on the Cell , and I'm trying to select a row at a time.

Edited: If you change SelectionUnit to CellOrRowHeader to allow cell selection, but binding to select an entire row

+5
source

Source: https://habr.com/ru/post/1437915/


All Articles