CollectionViewSource 过滤事件与属性
使用 CollectionViewSource.View.Filter 属性与使用 CollectionViewSource.Filter 事件之间有哪些实际差异?在某些情况下您会使用其中一种而不是另一种,或者这是一个偏好问题?
干杯,
贝里尔
编辑: 我确实看到文档说“如果您的视图对象来自 CollectionViewSource 对象,则可以通过为 Filter 事件设置事件处理程序来应用过滤逻辑。”尽管没有什么可以阻止您在视图上设置属性,并且它没有说明为什么要这样做。
到目前为止,我发现在 CollectionViewSource 上设置事件的优点是,您可以在一个事件处理程序中实现所有过滤逻辑,然后在用户更改过滤条件时使用 View.Refresh(或 View.DeferRefresh)调用它。
What are some of the practical differences between using the CollectionViewSource.View.Filter property as opposed to the CollectionViewSource.Filter event? Are there situations where you would use one over the other or is it a matter of preference?
Cheers,
Berryl
EDIT:
I do see that the docs say "If your view object comes from a CollectionViewSource object, you apply filtering logic by setting an event handler for the Filter event." although nothing stops you from setting the property on the view, and it doesn't say why to do so.
The advantage I have found so far in setting the event on the CollectionViewSource is that you can implement all of your filtering logic in one event handler and then use View.Refresh (or View.DeferRefresh) to invoke it as the user changes filtering criteria.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 CollectionViewSource 中设置
Filter
事件意味着即使不需要过滤也会调用该事件,这会降低该过程的效率。使用
Filter
事件的官方方法是在过滤时添加它,并在过滤器被清除时将其删除。viewsource.Filter += viewsource_Filter;
然后:
viewsource.Filter -= viewsource_Filter;
//你怎么知道有多少个事件!?如果您使用该事件,则必须确保每次过滤器值更改时都不会添加该事件,因为除了潜伏着多余的事件(=应用程序更加努力地工作)之外,您还必须删除以便清除过滤器。
因此,使用
Filter
属性有一个优点,因为通过将该属性设置为null
可以更轻松地清除过滤器。viewsource.view.Filter = null;
Setting the
Filter
event in the CollectionViewSource would mean that the event is called even when there is no filtering needed which would make the process less efficient.The official way of using the
Filter
event is by adding it on filtering and removing it later when the filter is cleared.viewsource.Filter += viewsource_Filter;
Then:
viewsource.Filter -= viewsource_Filter;
//how do you know how many events there are!?If you use the event, you have to make sure that you are not adding the event each time the filter value changes, because apart from having redundant events lurking around (=app works harder for nothing) you will have to remove all the events in order to clear the filter.
Thus, there is an advantage in using the
Filter
property, because you can more easily clear the filter by setting the property tonull
.viewsource.view.Filter = null;