Why is ReadOnlyObservableCollection.CollectionChanged not public?

后端 未结 8 2110
心在旅途
心在旅途 2021-01-31 06:50

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

What is the use of a col

8条回答
  •  别那么骄傲
    2021-01-31 07:23

    There are definitely good reasons for wanting to subscribe to collection changed notifications on a ReadOnlyObservableCollection. So, as an alternative to merely casting your collection as INotifyCollectionChanged, if you happen to be subclassing ReadOnlyObservableCollection, then the following provides a more syntactically convenient way to access the a CollectionChanged event:

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

    This has worked well for me before.

提交回复
热议问题