0
我想在WPF MVVM中创建一个具有信息行的数据网格,而列是代表Boolean
属性的DataGridCheckBoxColumn
。如何在一次点击中禁用行选择并启用复选框?
我希望能够点击一个复选框,并将其更改为“检查”在一次点击。 我也想禁用选项来选择行,也禁用选项来更改其他列中的其他内容。
请指教。
我想在WPF MVVM中创建一个具有信息行的数据网格,而列是代表Boolean
属性的DataGridCheckBoxColumn
。如何在一次点击中禁用行选择并启用复选框?
我希望能够点击一个复选框,并将其更改为“检查”在一次点击。 我也想禁用选项来选择行,也禁用选项来更改其他列中的其他内容。
请指教。
使用该答案为出发点:How to perform Single click checkbox selection in WPF DataGrid?
我做了一些修改,并结束了与此:
WPF:
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridRow_PreviewMouseLeftButtonDown"/>
</Style>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"/>
</Style>
</DataGrid.Resources>
后面的代码:
private void DataGridRow_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DataGridRow row = sender as DataGridRow;
if (row == null) return;
if (row.IsEditing) return;
if (!row.IsSelected) row.IsSelected = true; // you can't select a single cell in full row select mode, so instead we have to select the whole row
}
private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
if (cell == null) return;
if (cell.IsEditing) return;
if (!cell.IsFocused) cell.Focus(); // you CAN focus on a single cell in full row select mode, and in fact you HAVE to if you want single click editing.
//if (!cell.IsSelected) cell.IsSelected = true; --> can't do this with full row select. You HAVE to do this for single cell selection mode.
}
尝试一下,看看它是否做到了你想要的。