Tuesday, March 25, 2014

Sorting Evolution (7) - Sorting a WPF ListView/GridView by clicking on the header - Attached Property

Seventh generation

With the last post (Sixth Generation) we have improved our sorting mechanism that it can be easily reused in diverse projects. From that point no further changes are necessary. Nevertheless in this post we will use the developed sorting mechanism combined with Attached Property. This will remove the possibility to use the sorting mechanism within the ViewModel although sorting can be placed within XAML, only. Therefore we have to do some changes.

The Sorting class is refactored so that there is only one public method (Sort), the method SetAdorner is called from that. Determination of SortDirection is done only once in SetAdorner and is removed from Sort. The property SetData<T> and method GetView are removed. Sorting is initialized now from Attached Property (View side) not by ViewModel.

public void Sort(object columnHeader, CollectionView list)
    string column = SetAdorner(columnHeader);
        new SortDescription(column, _sortDirection));

To create a custom Attached Property a static DependencyProperty is defined (IsSortingProperty) by using the RegisterAttached method. This method has the possibility to set a PropertyMetadata as parameter in that a PropertyChangedCallback can be defined (OnSortingPropertyChanged) by using Delegates. In the callback method an event handler is added to/removed from ListView that is called on clicking on a column header. The event handler ensures that the column is sorted. In order that the Attached Property can be used with several ListView a Dictionary is used to store a Sorting object to each ListView object. Furthermore a static Get Accessor (GetIsSorting) and a static Set Accessor (SetIsSorting) is added.

private static IDictionary<ListViewSorting> _sorting;
static SortingAttached()
    _sorting = new Dictionary<ListViewSorting>();
public static readonly DependencyProperty IsSortingProperty =
    new PropertyMetadata(false, OnSortingPropertyChanged));
public static bool GetIsSorting(ListView element)
    return (bool)element.GetValue(IsSortingProperty);
public static void SetIsSorting(ListView element, Boolean value)
    element.SetValue(IsSortingProperty, value);
private static void OnSortingPropertyChanged(DependencyObject d,
    DependencyPropertyChangedEventArgs e)
    ListView listView = d as ListView;
    if (listView != null)
        if (e.NewValue is bool)
            if ((bool)e.NewValue)
                    new RoutedEventHandler(OnColumnHeaderClicked));
                _sorting.Add(listView, new Sorting());
                    new RoutedEventHandler(OnColumnHeaderClicked));
private static void OnColumnHeaderClicked(object sender, RoutedEventArgs e)
    ListView listView = sender as ListView;
    if (listView == null)
    var sorter = _sorting[listView];
    sorter.Sort(e.OriginalSource, listView.Items);

In the ViewModel Sorting is removed, so it contains only data as in the Second Generation.

public ObservableCollection<ResultData> SeventhResultData { getset; }

In the XAML part of the View the Click event is removed and the Attached Property is added.

<ListView x:Name="SeventhResultData"
   ItemsSource="{Binding SeventhResultData}">
     <GridViewColumn DisplayMemberBinding="{Binding ResultNumber}">
         <GridViewColumnHeader Content="Number"
          HorizontalContentAlignment="Left" />
     <GridViewColumn DisplayMemberBinding="{Binding ResultOutput}">
                <GridViewColumnHeader Content="Output"
                        HorizontalContentAlignment="Left" />

In the code-behind of the View nearly all code is removed, only DataContext is there set.

With all that changes Sorting can now be done by only adding Attached Property, but cannot be triggered from ViewModel.

The source code can be downloaded from http://code.msdn.microsoft.com/windowsdesktop/Sorting-a-WPF-ListView-by-027e2303

Further Posts

  1. Sorting a WPF ListView/GridView by clicking on the header
  2. Sort Direction Indicators 
  3. Sort in ViewModel 
  4. Sort Direction Indicators with Adorners
  5. Reusability
  6. More Reusability
  7. Attached Property
  8. Behaviors (Expression Blend)

No comments:

Post a Comment