Reputation: 326
I have custom sorting for IP column with reset. And I have reseting for other columns
public static void SortHandler(object sender, DataGridSortingEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;
string sortPropertyName = Helpers.GetSortMemberPath(e.Column);
if (!string.IsNullOrEmpty(sortPropertyName))
{
Console.WriteLine(sortPropertyName);
if (sortPropertyName == "Ip")
{
IComparer comparer = null;
e.Handled = true;
if (e.Column.SortDirection.HasValue && e.Column.SortDirection.Value == ListSortDirection.Descending)
{
e.Column.SortDirection = null;
}
else
{
ListSortDirection direction = (e.Column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending;
e.Column.SortDirection = direction;
comparer = new SortIPAddress(direction);
}
ListCollectionView lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(dataGrid.ItemsSource);
lcv.CustomSort = comparer;
}
// sorting is cleared when the previous state is Descending
if (e.Column.SortDirection.HasValue && e.Column.SortDirection.Value == ListSortDirection.Descending)
{
int index = Helpers.FindSortDescription(dataGrid.Items.SortDescriptions, sortPropertyName);
if (index != -1)
{
e.Column.SortDirection = null;
dataGrid.Items.SortDescriptions.RemoveAt(index);
dataGrid.Items.Refresh();
if ((Keyboard.Modifiers & ModifierKeys.Shift) != ModifierKeys.Shift)
{
dataGrid.Items.SortDescriptions.Clear();
dataGrid.Items.Refresh();
}
// stop the default sort
e.Handled = true;
}
}
}
}
But if I do double sorting with shift
, sorting for IP column resets.How to fix double sorting?
Forum ask for more details, but I do not know what else to add
Upvotes: 0
Views: 614
Reputation: 48139
If your data is coming from a collection of some class type, add a custom property that is combination of multiple fields...
public class YourClassType
{
public string SomeColumn {get; set;}
public int SomeInt {get; set; }
...
public string SortCombination { get { return SomeColumn + SomeSort; }}
}
Then in the xaml data grid column, set the "SortMemberPath" to that property name. Ex:
SortMemberPath = "SortCombination";
No extra "helper" to sort / double-sort... it uses the one column as the sort basis. You could even format the number to be sure properly justified same length depending on its content.
Upvotes: 0
Reputation: 435
Could you modify your code to add the sorting groups like so
DataG.SortDescriptions.Add(new SortDescription("col1", ListSortDirection.Ascending));
DataG.SortDescriptions.Add(new SortDescription("col2", ListSortDirection.Ascending));
which would apply sorting as you described.
Upvotes: 1