#989 – Enabling Live Filtering in a CollectionViewSource
January 17, 2014 5 Comments
Like sorting, filtering in a CollectionViewSource is not automatically done when you change the contents of one of the data bound items. By default, you need to call the Refresh method of the CollectionViewSource.
In the example below, we filter on first name “Joan” and then change Joan Fontaine’s first name to “Bob”. Notice that the list is not re-filtered–Bob remains in the list.
You can fix this by adding the FirstName property to the LiveFilteringProperties collection of the CollectionViewSource and setting IsLiveFilteringRequested to true.
<CollectionViewSource x:Key="cvsActors" Source="{Binding ActorList}" IsLiveFilteringRequested="True"> <CollectionViewSource.LiveFilteringProperties> <clr:String>FirstName</clr:String> </CollectionViewSource.LiveFilteringProperties> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName="LastName" /> </CollectionViewSource.SortDescriptions> </CollectionViewSource>
Now, when we change Joan to Bob and we’re filtering on “Joan”, Bob automatically disappears from the list.
what xmlns should be added to the heading of xaml to get the below accepted:
FirstName
Try:
xmlns:clr=”clr-namespace:System;assembly=mscorlib”
thanks. it worked
Where in your code do you specify that it’s filtering on “Joan”?
The CheckBox would bind to an OnlyJoans property that can then be checked in the Filter event handler for the CollectionViewSource. See this earlier post for an example – https://wpf.2000things.com/2014/01/14/986-filtering-a-listbox-using-a-collectionviewsource/