在我的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事件,但只有在我选择并取消选中包含该复选框的单元格时,该值才会更改。 为什么会这样?我怎样才能改变这种行为?
答案 0 :(得分:8)
原因是因为CellTemplate内部绑定的默认行为是UpdateSourceTrigger = LostFocus
更改为:
<DataGridCheckBoxColumn Binding="{Binding isFixedByBracket, NotifyOnTargetUpdated=True , UpdateSourceTrigger=PropertyChanged }" />