Question

I was wondering how I could update a filter on a CollectionViewSource with a ComboBox. I have the following code:

 <CollectionViewSource x:Key="cvsCars" 
                       Source="{Binding Source={StaticResource odpCars}}">
 <ComboBox Name="cbxMake" Margin="5" IsEnabled="False" />

I'm sure I need some sort of a SelectionChanged event for the ComboBox but I can't figure out a way to make it work with this code.

private void MakeFilterOn(object sender, RoutedEventArgs e)
{
    cbxMake.IsEnabled = true;
    cvsCars.Filter += new FilterEventHandler(cvsCars_Filter);
}

void cvsCars_Filter(object sender, FilterEventArgs e)
{
    Car car = e.Item as Car;
    if (car != null)
    {
        if (car.Maker.ToString() == cbxMake.SelectedItem.ToString())
        {
            e.Accepted = true;
        }
        else
        {
            e.Accepted = false;
        }
    }
}

Any advice is greatly appreciated.

The CollectionViewSource is populated by an ObjectDataProvider. The updates will be applied to a ListBox. MakeFilterOn is a CheckBox.

Was it helpful?

Solution

You'll have to refresh your CollectionViewSource's View...so, in your handler for your combobox's SelectionChanged event, refresh your cvs:

cvsCars.View.Refresh();

You may want to look into the databinding powers of WPF and then later, the Model View ViewModel (MVVM) "pattern". That way, you can bind the combobox's SelectedItem to a property on your window's DataContext and eliminate the need for handling the SelectionChanged event.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top