过滤 ObservableCollection具有多个标准
当我在 DataGrid 列上的 filter_textboxes 中输入数据时,如何过滤绑定到 WPF DataGrid 的 ObservableCollection?
有没有一个不使用 codeproject 库的简单解决方案...?
How can I filter an ObservableCollection bound to a WPF DataGrid when I enter data in the filter_textboxes over the DataGrid`s columns?
Is there an easy solution without using codeproject libs... ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我前段时间问过一个类似的问题,您可能会觉得有用:Filter WPF TreeView using MVVM
对于 DataGrid 来说,这应该是非常相似的方法。基本上,您希望使用 ObservableCollection 创建一个 CollectionViewSource 并将 DataGrid 绑定到它,而不是直接绑定到 ObservableCollection,然后只需设置
CollectionViewSource
的Filter
当用户键入时。I asked a similar question some time ago which you might find useful: Filter WPF TreeView using MVVM
It should be a very similar approach for DataGrid. Basically, you want to create a CollectionViewSource using your ObservableCollection and bind your DataGrid to that instead of binding directly to the ObservableCollection and then it's simply a matter of setting the
CollectionViewSource
'sFilter
when the user types.我有一个非常相似的问题,并且有一个相当简单的解决方案。简而言之:
要将多个过滤器应用于绑定到 WPF DataGrid 的集合,您应该实例化 CollectionViewSource< /a> 对象作为视图和集合之间的代理(这也适用于其他集合控件)。这样做将允许您向其 Filter 事件订阅多个过滤器事件处理程序。过滤器按照订阅的顺序应用,并且可以通过取消订阅来删除。
如果您在代码隐藏或 ViewModel 中使用 CollectionViewSource.GetDefaultView() 静态方法,这将返回 ICollectionView 的实例,该实例仅支持具有 Filter 属性的单个过滤器。
您可以在此处找到带有源代码的示例 http:// www.codeproject.com/Articles/442498/Multi-filtered-WPF-DataGrid-with-MVVM
I had a very similar problem and there is a fairly simple solution. In short:
To apply multiple filters to a collection bound to a WPF DataGrid you should instantiate a CollectionViewSource object as a proxy between the view and the collection (this will also work with other collection controls). Doing so will allow you to subscribe multiple filter event handlers to it's Filter event. Filters are applied in the order in which they are subscribed and can be removed by unsubscribing them.
If you used the CollectionViewSource.GetDefaultView() static method in your codebehind or ViewModel, this will return an instance of an ICollectionView which will only support a single filter with a Filter property.
Your can find an example with source code here http://www.codeproject.com/Articles/442498/Multi-filtered-WPF-DataGrid-with-MVVM