WPF:如何限制组合框中的项目数 [英] WPF: How do i limit number of items in Combobox

查看:91
本文介绍了WPF:如何限制组合框中的项目数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个WPF自定义组合框,它能够根据搜索字符串"过滤项目. ComboBox ItemsSource绑定到ObservableCollection.

ObservableCollection是"Person"对象的集合.它公开了一个使用计数"属性.

现在,如果搜索字符串"为空,则必须显示ObservableCollection中的前30条记录. 人员"类中的"UsageCount"属性确定前30条记录(即,必须显示最大使用计数的前30条记录). UsageCount属性动态更改.我该如何实现..请帮助.预先感谢:)

解决方案

如您所知,ObservableCollection 并不是在此工作的正确工具,那么您该怎么办?好吧,对我们来说幸运的是,WPF提供了称为CollectionView的东西,您可以将其视为集合的包装.为了使用它,请将要绑定的项目公开为ICollectionView项目,如下所示:

  public  ICollectionView MyView { 获取私有 设置; } 

现在,您可以像这样填充它:

 IList< Person> people = GetPeople();
最后,您只需要过滤数据(使用过滤此列表所需要的任何算法),然后使用

 MyView.Refresh(); 


我尚未测试,但我认为以下内容可能会有所帮助.

1.实现INotifyPropertyChanged 接口,并在UsageCount 属性的设置器中,调用PropertyChanged 事件处理程序方法.
2.在PropertyChanged 事件处理程序方法中,使用LINQ查询设置DataContext ,如此处 http所述. ://msdn.microsoft.com/zh-CN/library/bb910060.aspx [ ^ ]
3.可以使用以下LINQ query.

 .DataContext= Persons.OrderBy(p = >  p. UsageCount, endingdingOrderComparer()).Take( 30 );

// 假定UsageCount的类型为int.
// 对于降序比较,需要使用Comparer.
公共  DescendingOrderComparer:IComparer< int> {
公共  int  Compare( int 首先, int 秒){
返回秒-第一;
}
} 


I''ve created a WPF custom ComboBox which has the ability to filter items according to a "search string". The ComboBox ItemsSource is bound to a ObservableCollection.

The ObservableCollection is a collection of "Person" object. It exposes a property "Usage Count".

Now if the "search string" is empty i have to show the Top 30 records from the ObservableCollection. The "UsageCount" property in the "Person" class decides the Top 30 Records(i.e. the the Top 30 records with the maximum UsageCount has to be displayed). The UsageCount property changes dynamically. How do i achieve this.. Please help. Thanks in advance :)

解决方案

As you have rightly identified, the ObservableCollection is not quite the right tool for the job here, so what do you do? Well, fortunately for us, WPF provides something called the CollectionView which you can think of as a wrapper around a collection. In order to use it, expose the item that you want to bind on as an ICollectionView item like this:

public ICollectionView MyView { get; private set; }

Now, you fill it like this:

IList<Person> people = GetPeople();
MyView = CollectionViewSource.GetDefaultView(people);

Finally, you just need to filter the data (using whatever algorithm you need to filter this list on) and then update the ICollectionView using

MyView.Refresh();


I have not tested but I think the following may be helpful.

1. Implement INotifyPropertyChanged interface and in the setter of UsageCount property, call the PropertyChanged event handler method.
2. In the PropertyChanged event handler method set the DataContext using LINQ query as explained here http://msdn.microsoft.com/en-us/library/bb910060.aspx[^]
3. The following LINQ query can be used.

this.DataContext = Persons.OrderBy (p => p.UsageCount, new descendingOrderComparer()).Take(30);

//Assuming that the UsageCount is of type int.
//For descending order comparison, Comparer is required.
public class descendingOrderComparer  : IComparer<int> {
	public int Compare(int first, int second)  {
		return second - first;
	}
}


这篇关于WPF:如何限制组合框中的项目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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