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) {
OP solution
public void AddThing(IThing thing) {
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; }
Dan Nov 08 '17 at 14:20 2017-11-08 14:20
source share