wpfdata-bindingobservablecollectionlistcollectionview

ObservableCollection + ICollectionView = ObservableCollectionView?


I have a View Model that exposes an ObservableCollection. Now I want to add grouping and therefore want to use a View class, like e.g. the ListCollectionView. But that is not a generic type. Also, if I construct a ListCollectionView from an ObservableCollection, then it is not updated when the ObservableCollection is.

Did someone implement an ObservableCollection-backed View-collection somewhere?

If not, is it possible to achieve this dynamism somehow by using XAML?


Solution

  • You don't need a specific implementation ; a CollectionView is just a view of a collection. You can get the default CollectionView for a collection using CollectionViewSource.GetDefaultView:

    ObservableCollection<Something> collection = ...
    ICollectionView view = CollectionViewSource.GetDefaultView(collection);