I am developing an application using WPF mvvm approach. I have a requirement where I have to show a list of items in a combo box for selection. Based on some flag I need to filter out few items from the combo box for selection.
I tried to use two different items sources one with full list and another with filtered list and based on the flag I wanted to change the items source. This does not seem to be working well. Is there any easy way to apply filters on the existing list based on some flag ?
Although your question is not clear, but I think you don't need to maintain two list just to get the filter data. You can use simple LINQ to do the filtering. Suppose if you have a ViewModel Property like
And you want to filter that based on some bool values then you can write something like
Status can be the bool based on that you want to filter your data and you can add this bool inside your ComboBoxItem class.
There are lots of different ways to do this but my personal preference is to use a
ListCollectionView
as the ItemsSource of the control displaying the filtered list, to set a filter predicate onListCollectionView.Filter
and to callListCollectionView.Refresh
when the filter parameters change.The example below will filter a list of countries based on their continent.
Code
XAML