private void DataGridStandardSorting(object sender, DataGridSortingEventArgs e) { string sortPropertyName = SortHelpers.GetSortMemberPath(e.Column); if (!string.IsNullOrEmpty(sortPropertyName)) { // sorting is cleared when the previous state is Descending if (e.Column.SortDirection.HasValue && e.Column.SortDirection.Value == ListSortDirection.Descending) { int index = SortHelpers.FindSortDescription(Items.SortDescriptions, sortPropertyName); if (index != -1) { e.Column.SortDirection = null; // remove the sort description Items.SortDescriptions.RemoveAt(index); Items.Refresh(); if ((Keyboard.Modifiers & ModifierKeys.Shift) != ModifierKeys.Shift) { // clear any other sort descriptions for the multisorting case Items.SortDescriptions.Clear(); Items.Refresh(); } // stop the default sort e.Handled = true; } } } }
private void SortDescriptionsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { // clear all the headers of sort order foreach (DataGridColumn column in Columns) { string sortPath = SortHelpers.GetSortMemberPath(column); if (sortPath != null) { column.Header = sortPath; } } // add sort order int sortIndex = 0; foreach (SortDescription sortDesc in Items.SortDescriptions) { foreach (DataGridColumn column in Columns) { if (sortDesc.PropertyName == SortHelpers.GetSortMemberPath(column)) { var sb = new StringBuilder(); sb.Append(sortDesc.PropertyName); if (Items.SortDescriptions.Count > 1 && ShowSortOrder) { sb.Append(string.Format(" (Sort Order: {0})", sortIndex)); column.Header = sb.ToString(); } } } sortIndex++; } }