WPF DataGrid 过滤 - 刷新 CollectionViewSource 刷新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23081088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DataGrid Filtering - Refreshing CollectionViewSource Refreshing
提问by user3428422
I want to know how I can refresh a CollectionViewSource when a button is clicked?
我想知道如何在单击按钮时刷新 CollectionViewSource?
So far I have
到目前为止我有
<Window.Resources>
<CollectionViewSource x:Key="cvsCustomers"
Source="{Binding CustomerCollection}"
Filter="CollectionViewSource_Filter" >
</CollectionViewSource>
</Window.Resources>
Which creates the CollectionViewSource...
它创建了 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
哪个将源绑定到我的 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
谢谢
回答by Rohit Vats
Call Refresh()on Viewproperty of CollectionViewSourceto get it refreshed.
呼吁Refresh()在View财产CollectionViewSource得到它刷新。
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,然后在其视图上调用 refresh。
((CollectionViewSource)this.Resources["cvsCustomers"]).View.Refresh();

