WPF筛选列表框 [英] WPF Filter a ListBox

查看:112
本文介绍了WPF筛选列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ListBox中加载了一个字符串列表,现在我想在TextBox中输入文本时对其进行过滤.我该怎么办?

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
}

我将它与XAML绑定在一起:

ItemsSource ="{Binding ElementList}"

解决方案

CollectionViewSource类可以在这里提供帮助.据我所知,它具有过滤,排序和分组集合的许多功能.

ICollectionView view = CollectionViewSource.GetDefaultView(ElementList);
view.Filter = (o) => {return o;}//here is the lambda with your conditions to filter

当您不需要任何过滤器时,只需将view.Filter设置为null. 另请在过滤

I load a list of strings in my ListBox, now I want to filter it 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屋!

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