WPF DataGrid SelectedCellsChanged 获取“父 DataGrid 上 SelectionUnit 属性的当前值阻止选择行。”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12737747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DataGrid SelectedCellsChanged geting "The current value of the SelectionUnit property on the parent DataGrid prevents rows from being selected."
提问by Daler
Please help me, I am writing application with "WPF Application Framework" and EF Code First. I'm trying set selected row to ViewModels variable "SelectedRawMaterial" which is binded to DataGrids SelectedItem and it raises an exception : "The current value of the SelectionUnit property on the parent DataGrid prevents rows from being selected."
请帮助我,我正在使用“ WPF 应用程序框架”和 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));
}
}
}
In this place it raises 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 also I want to handle CellKeyDown.
我添加了 SelectionUnit="Cell" 因为我也想处理 CellKeyDown。
回答by Craig Suchanec
Its because you have the SelectionUnit(see the definition of the property)property of the data grid set to Celland I believe you are trying to select a row at a time.
这是因为您将数据网格的SelectionUnit(请参阅属性的定义)属性设置为,Cell并且我相信您正在尝试一次选择一行。
Edited:If you change SelectionUnitto CellOrRowHeaderto allow cell selection but the binding to select a whole row
编辑:如果您更改SelectionUnit为CellOrRowHeader允许选择单元格但绑定选择整行

