我怎样才能在后台线程执行WPF过滤器? [英] How can I execute WPF filters on a background thread?

查看:147
本文介绍了我怎样才能在后台线程执行WPF过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WPF中的过滤器,我遵循的模式提出的这里

I'm using filters in WPF and I'm following the pattern suggested here.

要总结,这涉及到被暴露在其重新presents作为筛选依据的文本视图模型text属性。当该属性设置(通过从视图中的文本框的结合),它使用 Col​​lectionViewSource.GetDefaultView(MyItems).Filter =等等来筛选项目的可见名单。

To summarize, this involves a text property being exposed on the ViewModel which represents the text to filter by. When the property is set (by the binding from the textbox in the View) it uses CollectionViewSource.GetDefaultView(MyItems).Filter = blah to filter the visible list of items.

这个伟大的工程,但问题是当项目集合是非常大的,因为过滤是前台线程上执行,因此挂起的用户界面。 是否存在对后台线程执行过滤的模式,以及如何做到这一点适合在模型 - 视图 - 视图模型模式?

This works great, but the problem comes when the collection of items is very large, as the filtering is performed on the foreground thread and thus hangs the UI. Does there exist a pattern for performing filtering on a background thread, and how does this fit in the Model-View-ViewModel pattern?

推荐答案

而不是使用的CollectionView过滤,做你自己:在你的ViewModel创建一个包含过滤后的数据,一个用于保持过滤器的属性。每当滤波器被改变,在一个单独的线程来计算新的滤波的数据属性然后当它完成更新属性断火工作项。当更新的属性,或更新现有的集合或替换它取决于很多的变化是如何制作的。

Instead of using CollectionView for filtering, do your own: In your ViewModel create a property that contains the filtered data and one to hold the filter. Whenever the filter is changed, fire off a work item in a separate thread to computed the new filtered data property then update that property when it is done. When updating the property, either update the existing collection or replace it depending on how many changes were made.

我使用I类使用自动完成此所以它是宣布一个集合被对方的过滤版本一样简单。

I use a class I use that automates this so it is as easy as declaring the one collection to be a filtered version of the other.

这篇关于我怎样才能在后台线程执行WPF过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆