WPF: Bind/Apply Filter on Boolean Property
- by Julian Lettner
I want to apply a filter to a ListBox accordingly to the IsSelected property of a CheckBox.
At the moment I have something like this.
XAML
<CheckBox Name="_filterCheckBox" Content="Filter list" Checked="ApplyFilterHandler"/>
<ListBox ItemsSource="{Binding SomeItems}" />
CodeBehind
public ObservableCollection<string> SomeItems { get; private set; }
private void ApplyFilterHandler(object sender, RoutedEventArgs e)
{
if (_filterCheckBox.IsChecked.Value)
CollectionViewSource.GetDefaultView(SomeItems).Filter += MyFilter;
else
CollectionViewSource.GetDefaultView(SomeItems).Filter -= MyFilter;
}
private bool MyFilter(object obj)
{
return ...
}
It works but this solution feels like the old-fashioned way (Windows Forms).
Question:
Is it possible to achieve this with Bindings / in XAML?
Thanks for your time.