WPF过滤ListBox

Kar*_*ann 11 c# wpf xaml listbox list

我加载了一个字符串列表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
}
Run Code Online (Sandbox Code Playgroud)

我在XAML中绑定它:

ItemsSource ="{Binding ElementList}"

Bla*_*ter 28

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

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

当您不需要任何过滤器时,只需设置view.Filternull.另请参阅有关过滤的文章

  • 啊!没什么比断开链接到其余答案的链接还好! (4认同)

Joh*_*son 8

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

using System;
using System.Windows;
using System.Windows.Controls;

public static class Filter
{
    public static readonly DependencyProperty ByProperty = DependencyProperty.RegisterAttached(
        "By",
        typeof(Predicate<object>),
        typeof(Filter),
        new PropertyMetadata(default(Predicate<object>), OnByChanged));

    public static void SetBy(ItemsControl element, Predicate<object> value)
    {
        element.SetValue(ByProperty, value);
    }

    public static Predicate<object> GetBy(ItemsControl element)
    {
        return (Predicate<object>)element.GetValue(ByProperty);
    }

    private static void OnByChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is ItemsControl itemsControl &&
            itemsControl.Items.CanFilter)
        {
            itemsControl.Items.Filter = (Predicate<object>)e.NewValue;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在xaml中使用如下:

<DataGrid local:Filter.By="{Binding Filter}"
          ItemsSource="{Binding Foos}">
    ...
Run Code Online (Sandbox Code Playgroud)

和viewmodel:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)