Search code examples
c#collectionviewsource

Why is `ICollectionView.MoveCurrentToNext` moving to items that are not in the filter range?


I have a CollectionView that is filtered by a predicate. When I debug this, I can see that the result view only contains the items that match the filter. However, when I use the MoveCurrentTo*()-methods the CurrentItem-pointer appears to move within the unfiltered source collection rather than the filtered one, i.e. it will not take me to the next(/previous/first/last) item matching my filter, but rather just the next one in the source collection - while still returning true which according to the documentation means that the new current item is supposedly within the view (which to my understanding it isn't). This surely can't be intended behaviour, can it? What could I be doing wrong?

The source collection in my case is an ObservableCollection instance called Entries. I have used CollectionViewSource.GetDefaultView(Entries) to obtain a reference to the default view (which I bound to a WPF ListView control via a CollectionViewSource) and then used new CollectionView(Entries) to create a secondary view. It is this secondary view that I'm having the problems with.


Solution

  • The problem apparently was that I was directly instantiating CollectionView rather than one of its derived classes like ListCollectionView. The former is a mere base class for collection views that does itself not implement advanced functionality like sorting, grouping and, as I now found out, filtering. Everything works as expected now after switching to ListCollectionView.

    P.S.: I was able to solve this after stumbling across this thread on MSDN, so thanks to Thomas Claudius Huber for the relevant pointer!