WPF DataGrid过滤 - 刷新CollectionViewSource刷新

时间:2014-04-15 10:30:36

标签: c# wpf xaml datagrid collectionviewsource

我想知道在单击按钮时如何刷新CollectionViewSource?

到目前为止我已经

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

创建CollectionViewSource ...

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

将源绑定到我的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;
            }
        }
    }

我的视图中的过滤器,

一切似乎都在工作(项目正在被绑定到网格)但是如何刷新视图或网格以便我可以再次激活上面的函数以便网格被过滤? (真的按一下按钮)

由于

1 个答案:

答案 0 :(得分:14)

Refresh() View 属性上调用 CollectionViewSource 以刷新它。

如果您想在按钮单击时执行此操作,则需要先从窗口资源访问CollectionViewSource,然后在其View上调用refresh。

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