Created
November 17, 2013 00:04
-
-
Save itajaja/7507120 to your computer and use it in GitHub Desktop.
TrulyObservableCollection fires collectionchanged also when items change
This file contains 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
using System; | |
using System.Collections.ObjectModel; | |
using System.Collections.Specialized; | |
using System.ComponentModel; | |
namespace Hylasoft.OrdersGui.Utils | |
{ | |
public class TrulyObservableCollection<T> : ObservableCollection<T> | |
where T : INotifyPropertyChanged | |
{ | |
public TrulyObservableCollection() | |
{ | |
CollectionChanged += TrulyObservableCollection_CollectionChanged; | |
} | |
void TrulyObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) | |
{ | |
if (e.NewItems != null) | |
{ | |
foreach (Object item in e.NewItems) | |
{ | |
(item as INotifyPropertyChanged).PropertyChanged += item_PropertyChanged; | |
} | |
} | |
if (e.OldItems != null) | |
{ | |
foreach (Object item in e.OldItems) | |
{ | |
(item as INotifyPropertyChanged).PropertyChanged -= item_PropertyChanged; | |
} | |
} | |
} | |
void item_PropertyChanged(object sender, PropertyChangedEventArgs e) | |
{ | |
var a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset); | |
OnCollectionChanged(a); | |
} | |
} | |
} |
Do you have the latest version of this excellent source?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your implementation is great and I did introduce new event too, at first. Then I discovered that this new event won't work with BindingOperations.EnableCollectionSynchronization(...), no queuing of those events on GUI thread would occur for example. This makes me feel that without some serious re-implementation the easiest solution is to inherit from NotifyCollectionChangedEventArgs and add "PropertyName" property. Next, use this new args class when rising CollectionChanged event and check for that type in the handlers.
And when rising the event:
And in handler: