如何对实体集合提供动态过滤?
我有一个继承 ObservableCollection 的类 ShipmentsCollection,其中包含装运对象(实体)。这显示在我的 ShipmentsView UserControl 上的列表框中。我的目的是允许用户在列表上方的文本框中输入内容,并使用包含该字符串的项目过滤列表,以及基于多个复选框和单选按钮的选项(交付状态和排序方向)进行过滤。
我已经尝试了几种方法,但没有一种看起来非常优雅或真正实用。我尝试过的事情如下:
- 将 ShipmentsCollection 放入 CollectionViewSource 中并通过谓词进行过滤。无法找出使过滤器根据用户输入或选项更改自动更新的好方法。
- 重构为继承 collectionViewSource 的类,并尝试直接在 XAML 中声明,但出现以下错误:“在配置中找不到指定的命名连接,不打算与 EntityClient 提供程序一起使用,或者无效”。尝试修复但找不到有效的解决方案。
- 重构为继承自 CollectionView,在代码隐藏的事件处理程序中实现过滤器逻辑。仍在尝试弄清楚如何在不命名 filtertext 文本框控件的情况下将过滤器字符串获取到事件处理程序。
任何人都对在 MVVM 设计模式中实现此功能有一些好主意。我预计列表中最多有 200 个对象,因此这不会是一个巨大的过滤操作。
科里
I have a class ShipmentsCollection that inherits ObservableCollection which contains shipment objects (entities). This is displayed in a listbox on my ShipmentsView UserControl. My intent is to allow a user to type into a textbox above the list and filter the list with items that contain that string, as well as filter based on several checkbox and radiobutton based options (Delivery status and orderby direction).
I have tried this several ways, but none seem very elegant or really functional. Things I have tried follows:
- Put ShipmentsCollection into a CollectionViewSource and filtered via predicate. Could not figure out a good way to make the filter auto update based on user typing or option change.
- Refactored as a Class that Inherits collectionViewSource and tried to declare directly in XAML but got the following error: "The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid". Tried fixing but could not find solution that worked.
- Refactored to inherit from CollectionView, implemented filter logic, in event handler in codebehind. Still trying to figure out how I can get the filter string to the event handler without naming the filtertext textbox control.
Anyone got some good ideas in regard to implementing this functionality in an MVVM design pattern. I expect to have at most 200 objects in the list, so it will not be an enormous filter operation.
Cory
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您的第一个选择。为了让自动过滤器根据键入工作,我会在 ViewModel 中执行类似 SearchString 属性的操作,将文本框文本绑定到该属性,并将绑定中的 UpdateSourceTrigger 设置为 PropertyChanged,以便它每次都会调用 SearchString PropertyChanged 事件键入一个键的时间,而不是等到该框失去焦点。
XAML:
ViewModel:将上述属性设置为 PropertyChanged 后,只要键入键,就会调用“Set”方法,而不仅仅是在文本框失去焦点时调用。
Your first option would be the one I would suggest. To get the auto-filter to work based on typing, I'd do something like a SearchString property in my ViewModel, bind the textbox text to that, and set the UpdateSourceTrigger in the binding to PropertyChanged so it will call the SearchString PropertyChanged event every time a key is typed instead of waiting until the box loses focus.
XAML:
ViewModel: With the above property set to PropertyChanged, the "Set" method gets called anytime a key is typed instead of just when the textbox loses focus.
我知道这个问题已经结束并且很老了。但对于像我这样寻找动态过滤的人,可以参考以下链接
https://github.com/lokeshlal/WPFDynamicFilters
上面的示例根据实体模型的属性上定义的属性为每个实体创建过滤器。
例如:
定义过滤器的属性
在模型属性中应用上述属性
定义将绑定到下拉类型的模型
ViewModel
的 模型
在上面的视图模型中,'FilterControlViewModel'属性将迭代模型的所有属性并收集属性的过滤器信息。
这个相同的属性将被分配给用户控件,如下面的 xaml 文件中所述
过滤器控件将获取所有属性并使用 itemscontrol 呈现控件
最后定义模板选择器
I know this question is closed and old. but for someone like me searching for dynamic filtering, can refer to the following link
https://github.com/lokeshlal/WPFDynamicFilters
the above example creates filters for each entity based on the attribute defined on property of entity model.
As an example:
Define an attribute for filters
Apply the above attribute in model properties
Define the model that will bind to the drop down type
ViewModel of actual View
In the above view model 'FilterControlViewModel' property will iterate all property of model and collect the filter information of the properties.
This same property will be assigned to the user control as explained in xaml file below
Filter control will take all the attributes and render the control using itemscontrol
Finally define the template selector