Filtering an Observable Collection in WPF MVVM via ICollectionView
I have an observabelCollection of a data object:
Each
GistViewModel
has the following:
In terms of what this represents, each Gist has a collection of GistFiles. I am mapping this onto a TreeViewControl (root being Gists, Child being GistFiles) via the following xaml (abridged):
This works successfully. Now I am trying to introduce a filter feature. I have followed a guide online somewhere which advises using ICollectionView
. I implement this thus:
And changing my xaml to:
I'm just trying to get the default list presented first. However, this does not work. No list items are displayed.
GistFilesToFirstFilenameConverter
doesn't get called when Binding = GistsView
The full code is here: https://github.com/stigzler/VisGist/blob/master/VisGist/ToolWindows/MainWindow.xaml
What am I missing?GitHub
VisGist/VisGist/ToolWindows/MainWindow.xaml at master · stigzler/Vi...
Contribute to stigzler/VisGist development by creating an account on GitHub.
2 Replies
I did not check your whole code, but I think the problem is this line:
This replaces the whole list object with a new one. Without the CollectionView this works, because the view was bound directly to this property and your setter called the PropertyChanged event via the SetProperty method.
When using an ObservableCollection the class already handles the PropertyChanged events itself. I usually just instantiate it once and then add or remove items from it. Omitting the setter will also prevent you from accidentally replacing the object. Like this:
Your LoadGistsAsync() method can return a standard list and the viewmodel can add them to the observable collection. Unfortunately ObservableCollection does not have an AddRange() method by default (though some frameworks like Prism provide one as an extension method).
thanks for the answer. Sadly, it didn't work - same problem 😦