wpf 数据网格:DataGridCheckBoxColumn 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19047696/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:44:42  来源:igfitidea点击:

wpf datagrid: DataGridCheckBoxColumn events

c#wpfxamlcheckbox

提问by FrancescoDS

In my WPF project I have build the following datagrid with checkbox column:

在我的 WPF 项目中,我使用复选框列构建了以下数据网格:

XAML

XAML

<DataGrid AutoGenerateColumns="False"  TargetUpdated="IsIntermediatePointFixedByBracketDataGrid_TargetUpdated">
        <DataGrid.Columns>               
            <DataGridCheckBoxColumn Binding="{Binding isFixedByBracket, NotifyOnTargetUpdated=True }" />
        </DataGrid.Columns>
</DataGrid>

C#

C#

private void IsIntermediatePointFixedByBracketDataGrid_TargetUpdated(object sender, DataTransferEventArgs e)
{
    DataGrid dg = (DataGrid)sender;
    if (dg.SelectedIndex != -1 
        && ((IsFixedByBracketElement)dg.SelectedItem).isFixedByBracket != this.currentIntermediatePosition.isFixedByBracket[dg.SelectedIndex])
    {
        this.currentIntermediatePosition.isFixedByBracket[dg.SelectedIndex] = 
                    ((IsFixedByBracketElement)dg.SelectedItem).isFixedByBracket;
    }
}

When I check/uncheck a checkbox, the TargetUpdated event is thrown, but the value changes only if I select and deselect the cell that contains the checkbox. Why does this happen?How can I change this behaviour?

当我选中/取消选中复选框时,会引发 TargetUpdated 事件,但仅当我选择和取消选择包含该复选框的单元格时,值才会更改。为什么会发生这种情况?我怎样才能改变这种行为?

回答by eran otzap

The Reason for this is becuase the default behavior of binding inside CellTemplates are UpdateSourceTrigger=LostFocus

这样做的原因是因为 CellTemplates 内绑定的默认行为是 UpdateSourceTrigger=LostFocus

change to :

改成 :

      <DataGridCheckBoxColumn Binding="{Binding isFixedByBracket, NotifyOnTargetUpdated=True , UpdateSourceTrigger=PropertyChanged }" />