WPF DataGrid Filtering - Refreshing CollectionViewSource Refreshing

限于喜欢 提交于 2019-12-04 23:46:52

问题


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


回答1:


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();


来源:https://stackoverflow.com/questions/23081088/wpf-datagrid-filtering-refreshing-collectionviewsource-refreshing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!