Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid Filtering - CollectionViewSource Refreshing

I want to know how I can refresh a CollectionViewSource when a button is clicked?

So far I have

<Window.Resources>
    <CollectionViewSource x:Key="cvsCustomers"
                          Source="{Binding CustomerCollection}" 
                          Filter="CollectionViewSource_Filter" >
    </CollectionViewSource>
</Window.Resources>

Which creates the CollectionViewSource...

<DataGrid HorizontalAlignment="Left" 
              Height="210" 
              Margin="47,153,0,0"
              VerticalAlignment="Top" Width="410"
              ItemsSource="{Binding Source={StaticResource cvsCustomers}}"
              CanUserAddRows="False"

Which binds the source to my Datagrid

    private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
    {
        Customer t = e.Item as Customer;
        if (t != null)
        // If filter is turned on, filter completed items.
        {
            if (t.Name.Contains(txtSearch.Text))
            {
                e.Accepted = true;
            }
            else
            {
                e.Accepted = false;
            }
        }
    }

And a filter in my View,

Everything seems to be working (items are being bounded to the grid) but how do I refresh the view or grid so I can fire of the above function again so the grid does get filtered? (by a button click really)

Thanks

like image 785
user3428422 Avatar asked Apr 15 '14 10:04

user3428422


1 Answers

Call Refresh() on View property of CollectionViewSource to get it refreshed.

In case you want to do it on button click, you need to access CollectionViewSource from window resources first and then call refresh on its View.

((CollectionViewSource)this.Resources["cvsCustomers"]).View.Refresh();
like image 194
Rohit Vats Avatar answered Sep 26 '22 00:09

Rohit Vats