WPF datagrid: DataGridCheckBoxColumn events

10,231

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

Change to :

<DataGridCheckBoxColumn Binding="{Binding isFixedByBracket, NotifyOnTargetUpdated=True ,
                        UpdateSourceTrigger=PropertyChanged }" />
Share:
10,231
FrancescoDS
Author by

FrancescoDS

Curiosity is the key to become a good programmer!

Updated on June 04, 2022

Comments

  • FrancescoDS
    FrancescoDS almost 2 years

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

    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;
        }
    }
    

    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 behavior?