如何使用IEditableObject对象在WPF DataGrid中执行单击复选框选择 [英] How to perform Single click checkbox selection in WPF DataGrid with IEditableObject object
问题描述
DataGridCheckBoxColumn的默认行为是用户必须单击两次以更改复选框的值。在如何在WPF DataGrid中执行单击复选框选择中主题有一些可行的解决方案,但是有一个问题-您在代码后面是否有一个viewmodel对象,该对象实现了 IEditableObject
接口,然后 EndEdit
方法不会执行。
The default behavior of DataGridCheckBoxColumn is that the user has to click twice to change the checkbox value. In the How to perform Single click checkbox selection in WPF DataGrid topic there are a couple of solutions which work, but there is a problem - is you have a viewmodel object in code behind, which implements the IEditableObject
interface, then the EndEdit
method doesn't execute.
任何想法如何使单击工作并保持 IEditableObject
功能?
Any idea how to make single click work and also preserve the IEditableObject
functionallity?
推荐答案
您可以处理 GotFocus
事件,并明确进入编辑模式并选中/取消选中 DataGrid
的 CheckBox
:
private void dg_GotFocus(object sender, RoutedEventArgs e)
{
DataGridCell cell = e.OriginalSource as DataGridCell;
if (cell != null && cell.Column is DataGridCheckBoxColumn)
{
dg.BeginEdit();
CheckBox chkBox = cell.Content as CheckBox;
if (chkBox != null)
{
chkBox.IsChecked = !chkBox.IsChecked;
}
}
}
<DataGrid x:Name="dg" AutoGenerateColumns="False" GotFocus="dg_GotFocus">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" />
...
这篇关于如何使用IEditableObject对象在WPF DataGrid中执行单击复选框选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!