Stopbyte

How to catch DataGrid cell content value changed event with SourceUpdated on UpdateSourceTrigger

Hi; i am trying to catch Cell value changed event on a WPF DataGrid, the needed behavior is that we need an event to fire every time user clicks on a CheckBox DataGrid cell, However CellChangedEvent and CellEditingEvent are not doing the requested job at all.

i currently do this:

<DataGrid ItemsSource="{Binding MyItemsCollection}" CanUserAddRows="false" AutoGenerateColumns="true" CurrentCellChanged="DataGrid_CurrentCellChanged">
    <DataGrid.Resources>
        <Style  TargetType="DataGridCell">
            <Style. Triggers>
                <Trigger  Property="IsMouseOver"  Value="true">
                    <Setter  Property="IsEditing"  Value="true" />
                </Trigger>
            </Style. Triggers>
        </Style>
    </DataGrid.Resources>
</DataGrid>

So the question is, how can I trigger an event when the user Checks/Unchecks the Checkboxes inside the cells?

thanks

2 Likes

I had the same issue, i solved it using something like this:

<EventSetter Event="CheckBox.Checked" Handler="InstallChecked" />

in the ElementStyle for the Columns i wanted, and i Implemented the InstallChecked handler in my code behind in C#.

1 Like
  1. In your DataGrid add a handler for TargetUpdated event.

  2. Set a column explicitly, and it would be nice if you set AutoGenerateColumns=False .

  3. In your Binding flag the NotifyOnTargetUpdated property (your target is your checkbox).

  4. In your Binding UpdateSourceTrigger=PropertyChanged and Mode=TwoWay (not the default behavior of the DataGrid).

XAML :

<DataGrid AutoGenerateColumns="False" TargetUpdated="DataGrid_TargetUpdated" ItemsSource="{Binding MyItemsSource}">
  <DataGrid.Columns>
    <DataGridCheckBoxColumn Width="200" Binding="{Binding  NotifyOnTargetUpdated=True,  Path=SomeBooleanProperty,  UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></DataGridCheckBoxColumn>
  </DataGrid.Columns>
</DataGrid>

Event handler code behind:

private void DataGrid_TargetUpdated(object  sender, DataTransferEventArgs  e)
{
    // handle it here!
}
2 Likes

That’s exactly what I was looking for thanks for the tip @sparta