How to do Sort / Filter / Group in your custom control

In one of my recent experiments, I was required to add sortingability to a custom control. The control itself displays a bunch of items just like the SlideDeckPanel. Additionally for each item Add / Remove the control also sorts the items automatically. Before I describe how I did it, let me talk a little bit about the control, just to set the context.

A little bit about the control

image

The SlideDeckControl (haven’t blogged about it yet!) is quite a control in itself. It behaves just like an ItemsControl but gives enormous flexibility to plugin your own ItemContainerGenerator (for virtualizing purposes), LayoutModes (similar to ItemsPanel but lot more flexible), Hover behaviors, Selection behaviors. Initially when I started out, the SlideDeckControl derived from ItemsControl. It had only one kind of virtualizing layout, so a single ItemContainerGenerator worked well. However, after some talks with our Design team, the requirements for the control changed drastically. The control was now supposed to do multiple layouts and dynamically switch between them (based on some criteria). Additionally the UI generation logic for each LayoutMode was slightly different and hence required multiple ItemContainerGenerators. Also as you would imagine the virtualizing logic was also different. On top of that the control had dynamic Hover and Selection behaviors and had to maintain these behaviors across the different layout possibilities.

Surely the ItemsControl was not going to work for me. Thus I began writing a new control that had a few features of ItemsControl and some extra stuff for my needs. In the process I had to build some of the logic for ItemsSource, ItemTemplate, etc.

We need Sorting, Filtering, Grouping

Soon enough I had to add one more feature related to data-handling. I had to do automatic SORT whenever an item got added / removed / changed. This was little tricky given the fact that I had already built up some code for data-manipulation. Initially I thought of plugging in some LINQ query and detect collection changes. That required more effort to keep the UI items in sync with the data items. After some research, the CollectionView turned out to be a savior. The following quote in the Remarks section of the MSDN docs caught my eye:

In WPF applications, all collections have an associated default collection view. Rather than working with the collection directly, the binding engine always accesses the collection through the associated view. To get the default view, use the CollectionViewSource.GetDefaultView method.

So by default when I set up a {Binding} for my ItemsSource, I get an instance of CollectionView (more precisely an ICollectionView). Why is this interesting and exciting ? CollectionView provides out-of-the-box support for Sorting, Filtering, Grouping. If I setup the SortDescriptions, Filter predicate and the GroupDescriptions, the CollectionView takes care of determining the right indexes for the items. I only have to listen to the CollectionChanged event on the CollectionView and make the necessary changes on my UI. Cool !

In Code…

The following snippet shows the property-changed-handler for the ItemsSourcedependency property:

 1 private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
 2 {
 3     SlideDeckControl control = d as SlideDeckControl;
 4     if (e.OldValue != null)
 5     {
 6     ICollectionView view = CollectionViewSource.GetDefaultView(e.OldValue);
 7     view.CollectionChanged -= control.ItemsSource_CollectionChanged;
 8     control.Items = null;
 9     }
10 
11     if (e.NewValue != null)
12     {
13     control.Items = new List<object>();
14     ICollectionView view = CollectionViewSource.GetDefaultView(e.NewValue);
15 
16     //FIX Adding a SortDescription just for test
17     view.SortDescriptions.Add(new SortDescription("SpineValue", ListSortDirection.Ascending));
18 
19     view.CollectionChanged += control.ItemsSource_CollectionChanged;
20     }
21 }
22 	
Note how the collection changed handler is setup on the CollectionView. In the Collection changed handler I look at the type of change and process accordingly:

 1 private void ItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
 2 {
 3     switch (args.Action)
 4     {
 5     case NotifyCollectionChangedAction.Add:
 6         OnItemsAdded(args.NewStartingIndex, args.NewItems);
 7         break;
 8     case NotifyCollectionChangedAction.Move:
 9         break;
10     case NotifyCollectionChangedAction.Remove:
11         OnItemsRemoved(args.OldStartingIndex, args.OldItems);
12         break;
13     case NotifyCollectionChangedAction.Replace:
14         break;
15     case NotifyCollectionChangedAction.Reset:
16         OnItemsReset();
17         break;
18     }
19 }
20 
21 private void OnItemsReset()
22 {
23     Items.Clear();
24 
25     NotifyUI(-1, NotifyCollectionChangedAction.Reset);
26 }
27 
28 private void OnItemsRemoved(int index, IList items)
29 {
30     int itemCount = items.Count;
31     for (int i = 0; i < itemCount; i++)
32     {
33     Items.RemoveAt(index);
34 
35     NotifyUI(index, NotifyCollectionChangedAction.Remove);
36     }
37 
38 }
39 
40 private void OnItemsAdded(int index, IList items)
41 {
42     int itemIndex = index;
43     foreach (object item in items)
44     {
45     // Add to Items collection
46     Items.Insert(itemIndex, item);
47 
48     // Prepare UI
49     NotifyUI(itemIndex, NotifyCollectionChangedAction.Add);
50 
51     itemIndex++;
52     }
53 }
54 	
NotifyUI() is a private function that looks at the current LayoutMode and passes control to it. The thing to note here is that the CollectionChanged event carries all the information regarding the final indexes of the items. So all one has to do is insert / remove at the given indexes. Can’t get any simpler !