Problem Statement
Imagine an ObservableCollection where the elements implement INotifyPropertyChanged and the collection itself monitors those elements for any changes. Although ObservableCollection
Solution
ObservableCollectionEx class
The proposed solution introduces ObservableCollectionEx
Usage
ObservableCollectionEx
ObservableCollectionEx<Element> collection = new ObservableCollectionEx<Element>(); ((INotifyPropertyChanged)collection).PropertyChanged += (x, y) => ReactToChange();
Notes
While this implementation fires the collection's PropertyChanged event when an element's property changes, this can be confusing. Therefore, an additional event can be introduced specifically for such changes.
Furthermore, the ObservableCollection
((INotifyPropertyChanged)collection).PropertyChanged += (x, y) => ReactToChange();
Note that unsubscribing from PropertyChanged requires setting the event handler to null:
collection.PropertyChanged -= (s, e) => { Trace.WriteLine("Changed " + e.PropertyName); };
The above is the detailed content of How Can I Create an ObservableCollection That Monitors Changes in its Element Properties?. For more information, please follow other related articles on the PHP Chinese website!