Why is ReadOnlyObservableCollection.CollectionChanged not public?

后端 未结 8 2089
心在旅途
心在旅途 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:34

    You might vote for the bug entry on Microsoft Connect that describes this issue: https://connect.microsoft.com/VisualStudio/feedback/details/641395/readonlyobservablecollection-t-collectionchanged-event-should-be-public

    Update:

    The Connect portal has been shutdown by Microsoft. So the link above does not work anymore.

    My Win Application Framework (WAF) library provides a solution: ReadOnlyObservableList class:

    public class ReadOnlyObservableList<T> 
            : ReadOnlyObservableCollection<T>, IReadOnlyObservableList<T>
    {
        public ReadOnlyObservableList(ObservableCollection<T> list)
            : base(list)
        {
        }
    
        public new event NotifyCollectionChangedEventHandler CollectionChanged
        {
            add { base.CollectionChanged += value; }
            remove { base.CollectionChanged -= value; }
        }
    
        public new event PropertyChangedEventHandler PropertyChanged
        {
            add { base.PropertyChanged += value; }
            remove { base.PropertyChanged -= value; }
        }
    }
    
    0 讨论(0)
  • 2021-01-31 07:37

    Here's the solution: CollectionChanged events on ReadOnlyObservableCollection

    You have to cast the collection to INotifyCollectionChanged.

    0 讨论(0)
提交回复
热议问题