views:

501

answers:

3

Why is ReadOnlyObservableCollection.CollectionChanged protected and not public (as the corresponding ObservableCollection.CollectionChanged is)?

What is the use of a collection implementing INotifyCollectionChanged if I can't access the CollectionChanged event?

+3  A: 

How you are going to use this event in READ-ONLY collection? I don't know what was the reason to implement INotifyCollectionChanged interface at the first view it looks illogical, but assuming the fact that reason really exists - it is pretty logical to make this events protected.

Edited: I've found a way for you of how to do this:

ObservableCollection<string> obsCollection = new ObservableCollection<string>();
INotifyCollectionChanged collection = new ReadOnlyObservableCollection<string>(obsCollection);
collection.CollectionChanged += new NotifyCollectionChangedEventHandler(collection_CollectionChanged);

You just need to refer to your collection explicitly by INotifyCollectionChanged interface.

Restuta
Please not that ReadOnlyObservableCollection is a wrapper around an ObservableCollection - which is going to change. Consumers of the ReadOnlyObservableCollection are only allowed to observe these changes, not change anything themselves.
Oskar
You shouldn't rely on this fact, read-only collection are not going to change in any case, cos it is called "read-only". This is my understanding.
Restuta
Look at the edited answer, I've got this =)
Restuta
+1 for the casting solution. But as for it not being logical to observe a read-only collection: If you had read only access to a database would you expect it to never change?
Daniel Skinner
Agree, also what does it mean "Obsevarable" then in the name of the type =) Microsoft framework developers were drunk, probably =)
Restuta
I don't see what the difficulty here is. The ReadOnlyObservableCollection is a class that provides a READ-ONLY way to observe a collection. If my class has a collection of, say, sessions, and I don't want people to be able to add or remove sessions from my collection, but still observe them, then ReadOnlyObservableCollection is the perfect vehicle for that. The collection is read only to the users of my class, but I have a read/write copy for my own use.
scwagner
+3  A: 

Here's the solution: CollectionChanged events on ReadOnlyObservableCollection

You have to cast the collection to INotifyCollectionChanged.

iik