Karl_Schuhmann Karl_Schuhmann - 1 year ago 224
C# Question

WPF Filter a ListBox

i load a list of strings in my ListBox, now i want do 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 binding it in xaml with


ItemsSource="{Binding ElementList}"

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download