CollectionView should support CollectionChanged events on a custom collection class. #29221
Replies: 2 comments
-
I've created a sample app here The app defines two collections, one derived from ReadOnlyCollection(ReadOnlyColorCollection) and a second (CustomReadOnlyColorCollection) that The UI presents two CollectionView instances and buttons to move an item from one to the other. Moving an item from left to right updates the left CollectionView but not the right CollectionView. Placing a breakpoint on CustomReadOnlyColorCollection.Add(NamedColor item) shows the item being added to the collection, the PropertyChanged event raised for Count and no subscribers to CollectionChanged. |
Beta Was this translation helpful? Give feedback.
-
I added a ListView to illustrate that CustomReadOnlyColorCollection works as expected. Since ListView is going to be obsolete in .Net 10 Maui, I would expect CollectionView to work here. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have two classes declared as implementing IReadOnlyList, INotifyCollectionChanged and INotifyPropertyChanged.
CollectionView will display the contents as expected and even support grouping;
however, it will NOT subscribe to CollectionChanged events for either class.
In contrast, if I create a class derived from ReadOnlyCollection then implement lNotifyCollectionChanged, CollectionView will subscribe to CollectionChanged events.
This suggests that CollectionView requires either IList or IList to enable CollectionChanged handling. (NOTE: I haven't yet teased out where the actual item binding occurs, so I haven't confirmed if this is the actual problem).
I've used this model with other classes, such as ListView, and various SyncFusion controls that support INotifyCollectionChanged with no issue.
Publically, the class is read-only but its contents can be updated internally or privately.
What am I missing?
FWIW: Using ReadOnlyCollection presents its own problems.
1: The public contract is NOT read-only. I never understood why this was designed this way.
2: Contains is not virtual.
To use ReadOnlyCollection, I end up having to create a custom IList and pass to the base constructor to enable support for INotifyCollectionChanged and Contains.
Beta Was this translation helpful? Give feedback.
All reactions