Reputation: 1354
I'm puzzled. I try using the PropertyChanged
event from an ObservableCollection
, but the compiler does not know the event. CollectionChanged
he knows. MSDN says the ObservableCollection
has the event (https://msdn.microsoft.com/en-us/library/ms653376.aspx). What am I doing wrong?
ObservableCollection<int> xx = new ObservableCollection<int>();
xx.PropertyChanged += (s, a) => { };
Upvotes: 3
Views: 1717
Reputation: 13600
PropertyChanged
is a protected event
, that's why it's not accessible from your code. As you surely know, protected
means it's accessible only from itself and from derived classes.
When it comes to ObservableCollection
, we have an event CollectionChanged
to know, when the collection changes (an item got deleted or added). If we need to know, if the item of the collection has been changed, you need to use a custom implementation, for example from here: TrulyObservableCollection
Also, as @O. R. Mapper correctly pointed out, there's is a way of doing this without creating a derived type. As ObservableCollection
implements INotifyPropertyChanged
explicitly, you have to cast the instance to the interface and then you can access properties, events and methods of the interface. So something like this works as well (but it's ugly to be honest):
ObservableCollection<int> xx = new ObservableCollection<int>();
((INotifyPropertyChanged)xx).PropertyChanged += (s, a) => { };
Upvotes: 5