WPF过滤ListBox

时间:2013-03-12 10:00:10

标签: c# wpf list xaml listbox

我在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}”

3 个答案:

答案 0 :(得分:28)

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

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

如果您不需要任何过滤器,只需将view.Filter设置为null即可。 另请参阅filtering

上的这篇文章

答案 1 :(得分:8)

以下是绑定过滤器的附加属性:

<DataGrid local:Filter.By="{Binding Filter}"
          ItemsSource="{Binding Foos}">
    ...

在xaml中使用如下:

public class ViewModel : INotifyPropertyChanged
{
    private string filterText;
    private Predicate<object> filter;

    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<Foo> Foos { get; } = new ObservableCollection<Foo>();

    public string FilterText
    {
        get { return this.filterText; }
        set
        {
            if (value == this.filterText) return;
            this.filterText = value;
            this.OnPropertyChanged();
            this.Filter = string.IsNullOrEmpty(this.filterText) ? (Predicate<object>)null : this.IsMatch;
        }
    }

    public Predicate<object> Filter
    {
        get { return this.filter; }
        private set
        {
            this.filter = value;
            this.OnPropertyChanged();
        }
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private bool IsMatch(object item)
    {
        return IsMatch((Foo)item, this.filterText);
    }

    private static bool IsMatch(Foo item, string filterText)
    {
        if (string.IsNullOrEmpty(filterText))
        {
            return true;
        }

        var name = item.Name;
        if (string.IsNullOrEmpty(name))
        {
            return false;
        }

        if (filterText.Length == 1)
        {
            return name.StartsWith(filterText, StringComparison.OrdinalIgnoreCase);
        }

        return name.IndexOf(filterText, 0, StringComparison.OrdinalIgnoreCase) >= 0;
    }
}

和viewmodel:

Hello {{$user}} \r\n
Welcome! \r\n

答案 2 :(得分:1)

如果将Dictionary as itemsource设置为listbox,请使用以下代码进行排序,

    private void tb_filter_textChanged(object sender, TextChangedEventArgs e)
    {
        Dictionary<string, string> dictObject = new Dictionary<string, string>();
        ICollectionView view = CollectionViewSource.GetDefaultView(dictObject);
        view.Filter = CustomerFilter;
        listboxname.ItemsSource = view;
    }

    private bool CustomerFilter(object item)
    {
        KeyValuePair<string, string> Items = (KeyValuePair<string,string>) item;
        return Items.Value.ToString().Contains("a");
    }

上面的代码返回了包含&#34; a&#34;。

的项目