ListBox Filter In WPF

Datetime:2016-08-23 03:55:21          Topic: WPF           Share

A couple of third party controls offer a way to filter the collection of the string which is most likely to be used in List Boxes or Combo Boxes or DataGrids.

Here is the basic example of how one can sort the List Box value, using the string entered in the textbox. (I would skip the basics of creating WPF Application part).

Have a List box (named lstEmps) with the valid string entries, as given below. For the reference, I am adding a couple of entries in the Constructor of my main Window.

List<string> TestStrings = new List<string>();  
lstTestFilter.Add("Developer- e1");  
lstTestFilter.Add("Senior - e2");  
lstTestFilter.Add("Mgr - e3");  
lstTestFilter.Add("Mgr - e3");  
lstTestFilter.Add("Developer- e1");  
lstTestFilter.Add("Developer- e1");  
lstTestFilter.Add("Developer- e1");  
lstEmps.ItemsSource = TestStrings;  

Afterwards, define an object of CollectionView, which is a very useful class (used to sort, filter the data). Set the filter method to the one which we want to use for string finding purposes. (Make sure this method needs to have a boolean as a return type and the parameter it takes is of type Object).

CollectionView view = CollectionViewSource.GetDefaultView(lstEmps.ItemsSource) as CollectionView;

view.Filter = CustomFilter; //<- This makes sure that every time there is an event trigger, it tries refreshing the view it needs to call the filter.

The custom filter is the substring finder method with true or false value. You can make slight modifications here in terms of the string comparison, where instead of IndexOf function, you can use String.Contains() function but remember, if you use Contains function, you will have to implement the code that handles ignoring the string cases.

private bool CustomFilter(object obj)   
{  
        if (string.IsNullOrEmpty(txtData.Text))   
        {  
            return true;  
        } else  
        {  
            return (obj.ToString().IndexOf(txtData.Text, StringComparison.OrdinalIgnoreCase) >= 0);  
        }  
}  

Here is the text changed event, which tries to refresh the list items on each keystroke, use enter.

private void txtData_TextChanged(object sender, TextChangedEventArgs e)   
{  
    CollectionViewSource.GetDefaultView(lstEmps.ItemsSource).Refresh();  
}  

How it works

On each keystroke, the filter method is called where obj is the value of the items in ItemsSource of the list box. It is called for all the items in the List box.

For each item in the list box’s ItemsSource, it checks if the text entered is present in the item which is being processed. If an item is present, it will only display the items containing the text. The others won’t be displayed.

Output:As initially, there is no data in the textbox, it will display all the items.

As soon as you hit the key characters, given below, you will only see the items containing the input string.

If you like to develop the similar behavior for the List View, please refer to the link .





About List