WPF过滤ListBox

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

 public void ListLoad() { ElementList = new List(); // 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类可以在这里提供帮助。 据我所知,它有很多function可以对集合进行过滤,排序和分组。

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

当您不需要任何filter时,只需将view.Filter设置为null 。 另请查看有关过滤的文章

这是绑定filter的附加属性:

 using System; using System.Windows; using System.Windows.Controls; public static class Filter { public static readonly DependencyProperty ByProperty = DependencyProperty.RegisterAttached( "By", typeof(Predicate), typeof(Filter), new PropertyMetadata(default(Predicate), OnWithChanged)); public static void SetBy(ItemsControl element, Predicate value) { element.SetValue(ByProperty, value); } public static Predicate GetBy(ItemsControl element) { return (Predicate)element.GetValue(ByProperty); } private static void OnWithChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { UpdateFilter((ItemsControl)d, (Predicate)e.NewValue); } private static void UpdateFilter(ItemsControl itemsControl, Predicate filter) { if (itemsControl.Items.CanFilter) { itemsControl.Items.Filter = filter; } } } 

在xaml中使用如下:

  ... 

和viewmodel:

 public class ViewModel : INotifyPropertyChanged { private string filterText; private Predicate filter; public event PropertyChangedEventHandler PropertyChanged; public ObservableCollection Foos { get; } = new ObservableCollection(); 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)null : this.IsMatch; } } public Predicate 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; } } 

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

  private void tb_filter_textChanged(object sender, TextChangedEventArgs e) { Dictionary dictObject = new Dictionary(); ICollectionView view = CollectionViewSource.GetDefaultView(dictObject); view.Filter = CustomerFilter; listboxname.ItemsSource = view; } private bool CustomerFilter(object item) { KeyValuePair Items = (KeyValuePair) item; return Items.Value.ToString().Contains("a"); } 

上面的代码返回包含“a”的项目。