WPF 过滤列表框 [英] WPF Filter a ListBox
本文介绍了WPF 过滤列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 ListBox
绑定到一个字符串列表.我想在 TextBox
中输入文本时过滤列表.我该怎么做?
public void ListLoad(){ElementList = new List();//创建一个字符串列表ElementList.Add("1");//添加一个字符串项ElementList.Add("2");//添加一个字符串项数据上下文 = 这个;//设置数据上下文}
我在 XAML 中绑定它:
ItemsSource="{Binding ElementList}"
解决方案
CollectionViewSource 类可以在这里提供帮助.据我所知,它有许多功能来过滤、排序和分组集合.
ICollectionView view = CollectionViewSource.GetDefaultView(ElementList);view.Filter = (o) =>{return o;}//这里是带有过滤条件的 lambda
当您不需要任何过滤器时,只需将 view.Filter
设置为 null
.另请查看有关过滤
I have a ListBox
with binding to a list of strings. I want to filter the list when I enter text in a TextBox
. How can I do it?
public void ListLoad()
{
ElementList = new List<string>(); // creation a list of strings
ElementList.Add("1"); // add a item of string
ElementList.Add("2"); // add a item of string
DataContext = this; // set the data context
}
I'm binding it in XAML with:
ItemsSource="{Binding ElementList}"
解决方案
CollectionViewSource class can help here. As far as I can tell it has many capabilities to filter, sort and group collections.
ICollectionView view = CollectionViewSource.GetDefaultView(ElementList);
view.Filter = (o) => {return o;}//here is the lambda with your conditions to filter
When you don't need any filter just set view.Filter
to null
.
Also check out this article on filtering
这篇关于WPF 过滤列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文