WPF: Bind/Apply Filter on Boolean Property

Posted by Julian Lettner on Stack Overflow See other posts from Stack Overflow or by Julian Lettner
Published on 2010-04-22T18:31:21Z Indexed on 2010/04/22 18:33 UTC
Read the original article Hit count: 365

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.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about binding