WPF ListBox and Creating Filtered Views

By Robbe Morris

Here's a quick tip for easily implemention of filtered ListBox views of an ObservableCollection. This really comes into play when you want to hide items in a ListBox of a third party control that has styling that is not hidden when the item in the datatemplate is hidden.

using System;
using System.Net;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Windows.Controls;
using System.ComponentModel;

namespace MyProject.MyLogic.ViewModel
{
    public class MyViewModel : INotifyPropertyChanged
    {

      public MyViewModel()
      {
           FilteredList.Filter += new FilterEventHandler(OnFilteredListChange);
           FilteredList.Source = HumanResources;
      }


      private ObservableCollection<MyCustomClass> _fullList = new ObservableCollection<MyCustomClass>();
      public ObservableCollection<MyCustomClass> FullList
      {
         get { return _fullList; }
         set
         {
            _fullList = value;
            OnPropertiesChanged("FullList");
          }
      }


      private CollectionViewSource _filteredList = new CollectionViewSource();
      public CollectionViewSource FilteredList
      {
          get
          {
             return _filteredList;
           }
           set
           {
               _filteredList = value;
           }
        }

     private void OnFilteredListChange(object sender, FilterEventArgs e)
     {
       if (e.Item != null)
       {
           var item = e.Item as MyCustomClass;
             e.Accepted = (item.IsVisible);
        }
       }

      private void OnListFiltered()  // Call this method after any change to the FullList collection or after filtering is done.
      {
           filteredList.View.Refresh();  
      }

       public void LoadFullList()
      {
           try
           {

                 _fullList.Clear();

                 var list = GetMyListOfCustomClasses();

                 if (list == null) return;

                 foreach (var item in list)
                 {
                     item.IsVisible = true;
                     _fullList.Add(item);
                 }

             }
             catch (Exception ex)
             {
                Debug.WriteLine(ex.Message);
             }
             finally
             {
                OnListFiltered();
             }
     }

     public void Filter(string description)
     {
         try
         {
             // write logic to set the .IsVisible property of the custom class.  
          }
           finally
          {
             OnListFiltered();
           }
     }

       public event PropertyChangedEventHandler PropertyChanged;

       public void OnPropertyChanged(string propertyName)
       {
             if (PropertyChanged == null) { return; }
           PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }



}
}

WPF ListBox and Creating Filtered Views  (1641 Views)