Created
June 23, 2016 23:24
-
-
Save kristopherjohnson/8b9c67601a4445c8a1b1b6c1b600e0ff to your computer and use it in GitHub Desktop.
TrulyObservableCollection. Credit: http://stackoverflow.com/a/5256827/1175
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public sealed class TrulyObservableCollection<T> : ObservableCollection<T> | |
| where T : INotifyPropertyChanged | |
| { | |
| public TrulyObservableCollection() | |
| { | |
| CollectionChanged += FullObservableCollectionCollectionChanged; | |
| } | |
| public TrulyObservableCollection(IEnumerable<T> pItems) : this() | |
| { | |
| foreach (var item in pItems) | |
| { | |
| this.Add(item); | |
| } | |
| } | |
| private void FullObservableCollectionCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) | |
| { | |
| if (e.NewItems != null) | |
| { | |
| foreach (Object item in e.NewItems) | |
| { | |
| ((INotifyPropertyChanged)item).PropertyChanged += ItemPropertyChanged; | |
| } | |
| } | |
| if (e.OldItems != null) | |
| { | |
| foreach (Object item in e.OldItems) | |
| { | |
| ((INotifyPropertyChanged)item).PropertyChanged -= ItemPropertyChanged; | |
| } | |
| } | |
| } | |
| private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e) | |
| { | |
| NotifyCollectionChangedEventArgs args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, sender, sender, IndexOf((T)sender)); | |
| OnCollectionChanged(args); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment