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

(afree) #1

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">
        <Style  TargetType="DataGridCell">
            <Style. Triggers>
                <Trigger  Property="IsMouseOver"  Value="true">
                    <Setter  Property="IsEditing"  Value="true" />
            </Style. Triggers>

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


(isoftech) #2

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#.

( #3
  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).


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

Event handler code behind:

private void DataGrid_TargetUpdated(object  sender, DataTransferEventArgs  e)
    // handle it here!

(afree) #4

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