DataGrid WPF Data Filtering - Updating the CollectionViewSource Collection

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

I still have

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

What creates a CollectionViewSource ...

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

What links 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 the filter in my view,

Everything seems to work (the elements are tied to the grid), but how to update the view or grid so that I can run this function again so that the grid is filtered out? (pressing a button really)

thank

+4
source share
1 answer

Call the property to update it. Refresh() View CollectionViewSource

, CollectionViewSource Windows, .

((CollectionViewSource)this.Resources["cvsCustomers"]).View.Refresh();
+10

Source: https://habr.com/ru/post/1536575/


All Articles