Why is ReadOnlyObservableCollection.CollectionChanged protected and not public (as the corresponding ObservableCollection.CollectionChanged is)?
What is the use of a col
ReadOnlyObservableCollection.CollectionChanged
is not exposed (for valid reasons outlined in other answers), so let's make our own wrapper class that exposes it:
/// A wrapped that exposes the internal "/>.
public class ObservableReadOnlyCollection : ReadOnlyObservableCollection
{
public new NotifyCollectionChangedEventHandler CollectionChanged;
public ObservableReadOnlyCollection(ObservableCollection list) : base(list) { /* nada */ }
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs args) =>
CollectionChanged?.Invoke(this, args);
}
People have asked why you would want to observe changes to a read-only collection, so I'll explain one of many valid situations; when the read-only collection wraps a private internal collection that can change.
Here's one such scenario:
Suppose you have a service that allows adding and removing items to an internal collection from outside the service. Now suppose you want to expose the values of the collection but you don't want consumers to manipulate the collection directly; so you wrap the internal collection in a ReadOnlyObservableCollection
.
Note that in order to wrap the internal collection with
ReadOnlyObservableCollection
the internal collection is forced to derive fromObservableCollection
by the constructor ofReadOnlyObservableCollection
.
Now suppose you want to notify consumers of the service when the internal collection changes (and hence when the exposed ReadOnlyObservableCollection
changes). Rather than rolling your own implementation you just want to expose the CollectionChanged
of the ReadOnlyObservableCollection
. Rather than forcing the consumer to make an assumption about the implementation of the ReadOnlyObservableCollection
, you simply swap the ReadOnlyObservableCollection
with this custom ObservableReadOnlyCollection
, and you're done.
The ObservableReadOnlyCollection
hides ReadOnlyObservableCollection.CollectionChanged
with it's own, and simply passes on all the collection changed events to any attached event handler.