3

在我的 WPF 项目中,我datagrid使用复选框列构建了以下内容:

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会引发该事件,但只有当我选择并取消选择包含该复选框的单元格时,该值才会更改。为什么会这样?我怎样才能改变这种行为?

4

1 回答 1

9

这样做的原因是因为内部绑定的默认行为CellTemplatesUpdateSourceTrigger=LostFocus.

改成 :

<DataGridCheckBoxColumn Binding="{Binding isFixedByBracket, NotifyOnTargetUpdated=True ,
                        UpdateSourceTrigger=PropertyChanged }" />
于 2013-09-27T10:15:32.837 回答