diff --git a/src/Umbraco.Core/Collections/EventClearingObservableCollection.cs b/src/Umbraco.Core/Collections/EventClearingObservableCollection.cs index 724261ed8f..af25cc1b4a 100644 --- a/src/Umbraco.Core/Collections/EventClearingObservableCollection.cs +++ b/src/Umbraco.Core/Collections/EventClearingObservableCollection.cs @@ -8,7 +8,7 @@ namespace Umbraco.Core.Collections /// Allows clearing all event handlers /// /// - public class EventClearingObservableCollection : ObservableCollection + public class EventClearingObservableCollection : ObservableCollection, INotifyCollectionChanged { public EventClearingObservableCollection() { @@ -22,12 +22,20 @@ namespace Umbraco.Core.Collections { } - // need to override to clear - public override event NotifyCollectionChangedEventHandler CollectionChanged; + // need to explicitly implement with event accessor syntax in order to override in order to to clear + // c# events are weird, they do not behave the same way as other c# things that are 'virtual', + // a good article is here: https://medium.com/@unicorn_dev/virtual-events-in-c-something-went-wrong-c6f6f5fbe252 + // and https://stackoverflow.com/questions/2268065/c-sharp-language-design-explicit-interface-implementation-of-an-event + private NotifyCollectionChangedEventHandler _changed; + event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged + { + add { _changed += value; } + remove { _changed -= value; } + } /// /// Clears all event handlers for the event /// - public void ClearCollectionChangedEvents() => CollectionChanged = null; + public void ClearCollectionChangedEvents() => _changed = null; } } diff --git a/src/Umbraco.Core/Collections/ObservableDictionary.cs b/src/Umbraco.Core/Collections/ObservableDictionary.cs index c7d1908d45..fd9e469f07 100644 --- a/src/Umbraco.Core/Collections/ObservableDictionary.cs +++ b/src/Umbraco.Core/Collections/ObservableDictionary.cs @@ -18,7 +18,7 @@ namespace Umbraco.Core.Collections /// /// The type of elements contained in the BindableCollection /// The type of the indexing key - public class ObservableDictionary : ObservableCollection, IReadOnlyDictionary, IDictionary + public class ObservableDictionary : ObservableCollection, IReadOnlyDictionary, IDictionary, INotifyCollectionChanged { protected Dictionary Indecies { get; } protected Func KeySelector { get; } @@ -77,13 +77,21 @@ namespace Umbraco.Core.Collections #endregion - // need to override to clear - public override event NotifyCollectionChangedEventHandler CollectionChanged; + // need to explicitly implement with event accessor syntax in order to override in order to to clear + // c# events are weird, they do not behave the same way as other c# things that are 'virtual', + // a good article is here: https://medium.com/@unicorn_dev/virtual-events-in-c-something-went-wrong-c6f6f5fbe252 + // and https://stackoverflow.com/questions/2268065/c-sharp-language-design-explicit-interface-implementation-of-an-event + private NotifyCollectionChangedEventHandler _changed; + event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged + { + add { _changed += value; } + remove { _changed -= value; } + } /// /// Clears all event handlers /// - public void ClearCollectionChangedEvents() => CollectionChanged = null; + public void ClearCollectionChangedEvents() => _changed = null; public bool ContainsKey(TKey key) {