在我的WPF项目中,我使用复选框列构建了以下数据网格:
XAML
<DataGrid AutoGenerateColumns="False" TargetUpdated="IsIntermediatePointFixedByBracketDataGrid_TargetUpdated">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding isFixedByBracket, NotifyOnTargetUpdated=True }" />
</DataGrid.Columns>
</DataGrid>
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;
}
}
当我选中/取消选中一个复选框时,将引发TargetUpdated事件,但是仅当我选择并取消选择包含该复选框的单元格时,该值才会更改。
为什么会发生这种情况?我该如何改变这种行为?
最佳答案
原因是因为CellTemplate内绑定的默认行为是UpdateSourceTrigger = LostFocus
改成 :
<DataGridCheckBoxColumn Binding="{Binding isFixedByBracket, NotifyOnTargetUpdated=True , UpdateSourceTrigger=PropertyChanged }" />