如何过滤ListView或ViewModel? [英] How to filter a ListView or ViewModel?

查看:94
本文介绍了如何过滤ListView或ViewModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户联系人的视图模型,它是双向数据绑定到ListView(也可以是ListBox / GridView)。

I have a view model for customer contacts that is two-way data binded to a ListView (could also be ListBox/GridView).

现在,我如何按字母顺序按升序/降序过滤ListView项? 另外,如何在以下情况下进行自动搜索?用户在TextBox中键入搜索字母?

Now, how could I filter the ListView items alphabetically ascending/descending? Also, how could I make a automatic search when e.g. user types search letters in a TextBox?

注意:项目没有以任何方式分组,并且视图模型是双向绑定的,因此我不确定是否可以为每次过滤重新查询/绑定整个视图模型...  最佳做法是在UI上进行任何过滤 - 或者查看
模型方?

NOTE: items are not grouped in any way and the view model is two-way binded so I am not sure is possible to re-query/bind the entire view model for each filtering...  Best practice do any filtering on either UI- or view model side?

在此先感谢

推荐答案

1.-一种可能的设计方法:

1.- A possible design approach:

- 你绑定到一个ObservableCollection< ...>一个
- 你准备一个排序列表< ...>两个基于TextBox.TextChanged事件

- 你将两个合并为一个没有One.Clear():这会产生一个'闪烁'效果;你可以使用Remove / Insert和小块来实现它,例如在
中的Dispatcher控件下演示
UI在ListView中添加项目时冻结
[条目:2012年12月25日星期二上午7:41]

- you bind to an ObservableCollection<...> One
- you prepare a sorted List<...> Two based on TextBox.TextChanged event
- you merge Two into One without One.Clear(): that would create a 'flicker' effect; you do it with Remove/Insert and in small chunks such as demonstrated under control of the Dispatcher in UI freezes while adding items inside the ListView [Entry : Tuesday, December 25, 2012 7:41 AM]

---

2.-另一种方式:

- 绑定到ObservableCollection< ...>一个
- 你准备一个已排序的ObservableCollection< ...>两个基于TextBox.TextChanged事件

- 你只需在ViewModel中通过赋值设置One,当然还有OnPropertyChange通知。

- you bind to an ObservableCollection<...> One
- you prepare a sorted ObservableCollection<...> Two based on TextBox.TextChanged event
- you set One from Two just by assignment in your ViewModel with - of course - a OnPropertyChange notification.

更简单如上所述,实际上没有闪烁。

Much simpler and practically flicker free as in above approach.

---

1和2之间的差异在于两个数据量。如果您有大量卷,则需要方法1,因为数据将在非UI线程中准备,并通过使用小块数据在UI线程中进行整合。但是,使用小的
卷和有效的排序算法,您可以在UI线程中准备好两个。然后,在没有进一步麻烦的情况下授权分配二对一。 

The difference between 1 and 2 resides in the volume of data in Two. If you have large volumes, you'd need approach 1 since data would be prepared in a non-UI thread and consolidated in the UI-thread by using small chunks of data. However with small volumes and efficacious sort algorithm, you could possibly afford to prepare Two in the UI-thread. Assigning Two to One would then be authorized without further ado. 


这篇关于如何过滤ListView或ViewModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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