5

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.

Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224

4 Answers4

9

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.

steveybrown
  • 293
  • 2
  • 11
  • How would this work on a `AutoGenerateColumns="True"` though? Looks like in order for this to be possible, we need to specify each column explicitly. – Jeff Feb 14 '20 at 17:18
0

If you need to know whether the edited DataGrid item belongs to a particular collection, you could do something like this in the DataGrid's RowEditEnding event:

    private void dg_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
    {
        // dg is the DataGrid in the view
        object o = dg.ItemContainerGenerator.ItemFromContainer(e.Row);

        // myColl is the observable collection
        if (myColl.Contains(o)) { /* item in the collection was updated! */  }
    }
friskm
  • 366
  • 3
  • 7
0

I used "CurrentCellChanged" instead.

    <DataGrid
        Grid.Row="1"
        HorizontalAlignment="Center"
        AutoGenerateColumns="True"
        AutoGeneratingColumn="OnAutoGeneratingColumn"
        ColumnWidth="auto"
        IsReadOnly="{Binding IsReadOnly}"
        ItemsSource="{Binding ItemsSource, UpdateSourceTrigger=PropertyChanged}">
        <b:Interaction.Triggers>
            <!--  CellEditEnding  -->
            <b:EventTrigger EventName="CurrentCellChanged">
                <b:InvokeCommandAction Command="{Binding CellEditEndingCmd}" />
            </b:EventTrigger>
        </b:Interaction.Triggers>
    </DataGrid>
kxiaocai
  • 2,803
  • 1
  • 12
  • 8
-1

You should just add an event handler on your ObservableCollection's CollectionChanged event.

Code snippet:

_listObsComponents.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ListCollectionChanged);

// ...


    void ListCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        /// Work on e.Action here (can be Add, Move, Replace...)
    }

when e.Action is Replace, this means that an object of your list has been replaced. This event is of course triggered after the changes were applied

Have fun!

Damascus
  • 6,553
  • 5
  • 39
  • 53
  • In fact, I did just that before I posted the question. CollectionChanged event does not get triggered when the user modifies the collection – Arsen Zahray Apr 30 '12 at 18:20
  • This is not your answer, but, CollectionChanged reports only if somehow an item is added or removed. Chances are the grid let you modify an item without really altering the collection itself, and so not firing the aforementioned event. – NestorArturo Apr 30 '12 at 18:51
  • Woops, yes, misunderstanding here, `CollectionChanged` will be fired when a full `Item` will change (ie. you put a new Item() instead of the previous). You'd need your `Item` class to implement `INotifyPropertyChanged` if you want to catch every modification :) – Damascus Apr 30 '12 at 18:56