Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid CellEditEnded event

I'm looking to know every time the user has edited a content of my DataGrid's cell. There's CellEditEnding event, but its called before any changes were made to the collection, that the DataGrid is bound to.

My datagrid is bound to ObservableCollection<Item>, where Item is a class, automatically generated from WCF mex endpoint.

What is the best way to know every time the user has committed the changes to the collection.

UPDATE

I've tried CollectionChanged event, end it does not get triggered when Item gets modified.

like image 882
Arsen Zahray Avatar asked Apr 30 '12 18:04

Arsen Zahray


1 Answers

You can use UpdateSourceTrigger=PropertyChangedon the binding of the property member for the datagrid. This will ensure that when CellEditEnding is fired the update has already been reflected in the observable collection.

See below

<DataGrid SelectionMode="Single"
          AutoGenerateColumns="False"
          CanUserAddRows="False"
          ItemsSource="{Binding Path=Items}" // This is your ObservableCollection
          SelectedIndex="{Binding SelectedIndexStory}">
          <e:Interaction.Triggers>
              <e:EventTrigger EventName="CellEditEnding">
                 <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding EditStoryCommand}"/> // Mvvm light relay command
               </e:EventTrigger>
          </e:Interaction.Triggers>
          <DataGrid.Columns>
                    <DataGridTextColumn Header="Description"
                        Binding="{Binding Name, UpdateSourceTrigger=PropertyChanged}" /> // Name is property on the object i.e Items.Name
          </DataGrid.Columns>

</DataGrid>

UpdateSourceTrigger = PropertyChanged will change the property source immediately whenever the target property changes.

This will allow you to capture edits to items as adding an event handler to the observable collection changed event does not fire for edits of objects in the collection.

like image 159
steveybrown Avatar answered Nov 03 '22 08:11

steveybrown