简体   繁体   中英

WPF double sorting in DataGrid

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM