C# – Why does ObservableCollection hide INotifyPropertyChanged.PropertyChanged

cdata-bindingobservablecollectionwpfxaml

I have a class that derives from ObservableCollection. I wanted to add some properties to this class (not the items in the collection) but have discovered that its implementation of PropertyChanged is protected. Since its protected I assume that is why my attempts at binding to these new properties in XAML are failing.

Short of creating an object that hangs off this class that implements INotifyPropertyChanged and houses these new properties, is there a way around this that I'm overlooking?

Best Answer

If the class actually implements the interface but does so explicitly and places access modifiers on the concrete implementation, all you need to do is get that object in the context of the interface itself and you can use it. For example..

ObservableCollection collection;

collection.PropertyChanged += ...;

That might fail, since its implementation of PropertyChanged is protected. You could, however, do this:

ObservableCollection collection;

INotifyPropertyChanged iproperty = collection;

iproperty.PropertyChanged += ...;

This should work, as you're dealing with the object as an instance of the interface, rather than just accessing interface members through the class definition.

I haven't done any WPF, so I have no idea if ObservableCollection actually implements INotifyPropertyChanged, but if it does then this should work.

Related Topic