ListBox Filter In WPF

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.

  1. List<string> TestStrings = new List<string>();  
  2. lstTestFilter.Add("Developer- e1");  
  3. lstTestFilter.Add("Senior - e2");  
  4. lstTestFilter.Add("Mgr - e3");  
  5. lstTestFilter.Add("Mgr - e3");  
  6. lstTestFilter.Add("Developer- e1");  
  7. lstTestFilter.Add("Developer- e1");  
  8. lstTestFilter.Add("Developer- e1");  
  9. 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.
  1. private bool CustomFilter(object obj)   
  2. {  
  3.         if (string.IsNullOrEmpty(txtData.Text))   
  4.         {  
  5.             return true;  
  6.         } else  
  7.         {  
  8.             return (obj.ToString().IndexOf(txtData.Text, StringComparison.OrdinalIgnoreCase) >= 0);  
  9.         }  
  10. }  
Here is the text changed event, which tries to refresh the list items on each keystroke, use enter.
  1. private void txtData_TextChanged(object sender, TextChangedEventArgs e)   
  2. {  
  3.     CollectionViewSource.GetDefaultView(lstEmps.ItemsSource).Refresh();  
  4. }  
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.

Output

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

Output  Output

Output

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

 

Up Next
    Ebook Download
    View all
    Learn
    View all