For a WPF DataGrid, an ObservableCollection <> data source is bound. Columns are set as normal DataGridTextColumn. After editing a cell, if you press Enter, for example, the property from the corresponding row changes, but the CollectionChanged event handler of the source collection is not called. How to catch the moment when after changing the cell text the property in the related object changes?
1 answer
The CollectionChanged event is dispatched only when the collection itself is changed, that is, by adding / deleting / replacing whole elements. When changes are made to the elements themselves, CollectionChanged not sent.
To catch changes inside an element, you need (when you add this element) to subscribe to the PropertyChanged event, if your data type of the element supports the INotifyPropertyChanged interface. However, the DataGrid does this on its own, you shouldn’t need to take care of it.
If your element does not implement INotifyPropertyChanged , then there is no normal method to catch changes inside the element. Therefore, the objects used as a data source for UI controls are practically required to implement INotifyPropertyChanged .
- You can think about how to make a class for the model class Facade, in which to implement the
INotifyPropertyChanged- Bulson - @Bulson: Yeah, and call it a
VMfacade :-) - VladD