Why is ReadOnlyObservableCollection.CollectionChanged protected and not public (as the corresponding ObservableCollection.CollectionChanged is)?
What is the use of a col
As answered already, you have two options: you can either cast the ReadOnlyObservableCollection
to the interface INotifyCollectionChanged
to access the explicitly implemented CollectionChanged
event, or you can create your own wrapper class that does that once in the constructor and just hooks up the events of the wrapped ReadOnlyObservableCollection
.
Some additional insights into why this issue has not been fixed yet:
As you can see from the source code, ReadOnlyObservableCollection
is a public, non-sealed (i. e. inheritable) class, where the events are marked protected virtual
.
That is, there might be compiled programs with classes that are derived from ReadOnlyObservableCollection
, with overridden event definitions but protected
visibility. Those programs would contain invalid code once the event's visiblity is changed to public
in the base class, because it is not allowed to restrict the visibility of an event in derived classes.
So unfortunately, making protected virtual
events public
later on is a binary-breaking change, and hence it will not be done without very good reasoning, which I am afraid "I have to cast the object once to attach handlers" simply isn't.
Source: GitHub comment by Nick Guererra, August 19th, 2015