C# WPF DataGrid单击复选框未进入编辑模式

C# WPF DataGrid单击复选框未进入编辑模式,c#,wpf,checkbox,datagrid,C#,Wpf,Checkbox,Datagrid,我有一个DataGrid,带有复选框-类型的列,该列应该可以通过单击进行编辑。这可以通过使用复选框轻松实现: <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" /> </DataT

我有一个
DataGrid
,带有
复选框
-类型的列,该列应该可以通过单击进行编辑。这可以通过使用
复选框
轻松实现:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <CheckBox IsChecked="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <CheckBox BackGround="Red" IsChecked="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" />
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
我还尝试过在没有任何运气的情况下处理
Selected
GotFocus
CheckBox。Checked
事件也不能使用,因为它们在重新加载时触发


如果发生
Selected
事件,问题在于,即使只在一列上处理该事件,它也会在所有列上启用单击编辑(同样,通过样式设置):


应该处理的事件是
PreviewMouseLeftButtonUp
,与Down事件的情况相同,
BeginEdit()
被称为原始的
复选框后,该复选框不再存在,无法处理取消/检查。双击问题的解决方案是找到复选框的新表示形式,然后重新引发事件threre或手动切换它:

private void DataGridCell_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (sender is DataGridCell cell && !cell.IsEditing && e.OriginalSource is UIElement source)
    {
        var actualSource = source is CheckBox ? 
            (CheckBox)source : VisualExtensions.GetVisualParent<CheckBox>(source);

        if (actualSource != null)
        {
            ItemListDG.BeginEdit();

            var newSource = cell.GetVisualChild<CheckBox>();
            if (newSource != null)
            {
                newSource.IsChecked = !newSource.IsChecked;
            }
        }      

    }
}
private void DataGridCell\u PreviewMouseLeftButtonUp(对象发送器,鼠标按钮ventargs e)
{
if(发送方为DataGridCell单元格&&!cell.IsEditing&&e.OriginalSource为UIElement源)
{
var actualSource=源是否为复选框?
(复选框)来源:visualParent.GetVisualParent(来源);
if(实际源!=null)
{
ItemListDG.BeginEdit();
var newSource=cell.GetVisualChild();
if(newSource!=null)
{
newSource.IsChecked=!newSource.IsChecked;
}
}      
}
}

当您处于编辑模式时,
CellTemplate
中的控件不可见。这应该是一个只读控件。你看到了吗?@mm8是的,我看到了,并尝试处理
Selected
事件,它破坏了其他类型的交互。在选中
的情况下,它是全局的,即使您仅将其附加到一列,它也会在单击其他列时触发,导致对每一列进行单击编辑。您可以使用
DataGridCell
属性来确定单击了哪一列,这也是我的想法之一,事实证明,事件参数没有实际的源/原始源/发送方信息,这三个参数都设置为
DataGridCell
,在其上附加
Selected
,而不管单击了哪个单元格。
DataGridCell
是单击的单元格,不是吗?请举例说明你失败的地方。
private void DataGridCell_Selected(object sender, RoutedEventArgs e)
{
    // second part of condition is always true, no matter what cell is clicked
    if (sender is DataGridCell cell && cell.Column == ItemListDG.Columns[0])
    {
        // try to detect if the CheckBox column was clicked, if not return
        if (sender != e.OriginalSource) // always false
            return;

        ItemListDG.BeginEdit(e); // always executes no matter the source
    }
}
private void DataGridCell_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (sender is DataGridCell cell && !cell.IsEditing && e.OriginalSource is UIElement source)
    {
        var actualSource = source is CheckBox ? 
            (CheckBox)source : VisualExtensions.GetVisualParent<CheckBox>(source);

        if (actualSource != null)
        {
            ItemListDG.BeginEdit();

            var newSource = cell.GetVisualChild<CheckBox>();
            if (newSource != null)
            {
                newSource.IsChecked = !newSource.IsChecked;
            }
        }      

    }
}