Why is ReadOnlyObservableCollection.CollectionChanged not publicly available?

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

What is the use of a collection that implements INotifyCollectionChanged if I cannot access the CollectionChanged event?

+62
collections c #
Jan 13 '10 at 16:08
source share
6 answers

Here's the solution: CollectionChanged events in ReadOnlyObservableCollection

You must distinguish the collection from INotifyCollectionChanged .

+42
Jan 15 '10 at 11:44
source share

I found a way for you 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 explicitly reference your collection using the INotifyCollectionChanged interface.

+10
Jan 13 '10 at 16:16
source share
+5
04 Feb 2018-11-21T00:
source share

There are definitely good reasons to sign up for the collection of modified notifications on ReadOnlyObservableCollection . Thus, as an alternative, simply using your collection as INotifyCollectionChanged , if you are a subclass of ReadOnlyObservableCollection , then the following provides a more syntactically convenient way to access a CollectionChanged event :

  public class ReadOnlyObservableCollectionWithCollectionChangeNotifications<T> : ReadOnlyObservableCollection<T> { public ReadOnlyObservableCollectionWithCollectionChangeNotifications(ObservableCollection<T> list) : base(list) { } event System.Collections.Specialized.NotifyCollectionChangedEventHandler CollectionChanged2 { add { CollectionChanged += value; } remove { CollectionChanged -= value; } } } 

This has worked for me before.

+5
04 Oct 2018-11-22T00:
source share

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

In other words, reading is just not immutable !!!!

Aside, ReadOnlyObservableCollection must implicitly implement INotifyCollectionChanged .

+5
Jan 30 '14 at 16:48
source share

It was the best hit on Google, so I decided that I would add my solution in case other people viewed it.

Using the above information (about the need to cast to INotifyCollectionChanged ), I made two extension methods for registering and unregistering.

My solution is extension methods

 public static void RegisterCollectionChanged(this INotifyCollectionChanged collection, NotifyCollectionChangedEventHandler handler) { collection.CollectionChanged += handler; } public static void UnregisterCollectionChanged(this INotifyCollectionChanged collection, NotifyCollectionChangedEventHandler handler) { collection.CollectionChanged -= handler; } 

Example

IThing.cs

 public interface IThing { string Name { get; } ReadOnlyObservableCollection<int> Values { get; } } 

Using extension methods

 public void AddThing(IThing thing) { //... thing.Values.RegisterCollectionChanged(this.HandleThingCollectionChanged); } public void RemoveThing(IThing thing) { //... thing.Values.UnregisterCollectionChanged(this.HandleThingCollectionChanged); } 

OP solution

 public void AddThing(IThing thing) { //... INotifyCollectionChanged thingCollection = thing.Values; thingCollection.CollectionChanged += this.HandleThingCollectionChanged; } public void RemoveThing(IThing thing) { //... INotifyCollectionChanged thingCollection = thing.Values; thingCollection.CollectionChanged -= this.HandleThingCollectionChanged; } 

Alternative 2

 public void AddThing(IThing thing) { //... (thing.Values as INotifyCollectionChanged).CollectionChanged += this.HandleThingCollectionChanged; } public void RemoveThing(IThing thing) { //... (thing.Values as INotifyCollectionChanged).CollectionChanged -= this.HandleThingCollectionChanged; } 
0
Nov 08 '17 at 14:20
source share



All Articles