Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ReadOnlyObservableCollection.CollectionChanged not public?

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?

like image 537
Oskar Avatar asked Jan 13 '10 16:01

Oskar


3 Answers

Here's the solution: CollectionChanged events on ReadOnlyObservableCollection

You have to cast the collection to INotifyCollectionChanged.

like image 101
Nick Avatar answered Oct 16 '22 22:10

Nick


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.

like image 30
Restuta Avatar answered Oct 16 '22 21:10

Restuta


I know this post is old, however, people should take their time to understand the patterns used in .NET before commenting. A read only collection is a wrapper on an existing collection that prevents consumers from modifying it directly, look at ReadOnlyCollection and you will see that it is a wrapper on a IList<T> which may or may not be mutable. Immutable collections are a different matter and are covered by the new immutable collections library

In other words, read only is not the same as immutable!!!!

That aside, ReadOnlyObservableCollection should implicitly implement INotifyCollectionChanged.

like image 37
Jason Young Avatar answered Oct 16 '22 22:10

Jason Young