WPF DataGrid中的双重排序

时间:2019-01-15 11:08:29

标签: c# wpf sorting datagrid

我对带有重置的IP列进行了自定义排序。而且我要重置其他列

enter image description here

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;
                }
            }
        }             
    }

但是,如果我使用shift进行双重排序,则会重置IP列的排序。如何解决双重排序? 论坛要求提供更多详细信息,但我不知道还要添加什么

2 个答案:

答案 0 :(得分:0)

您可以修改代码以添加排序组吗

DataG.SortDescriptions.Add(new SortDescription("col1", ListSortDirection.Ascending));
DataG.SortDescriptions.Add(new SortDescription("col2", ListSortDirection.Ascending));

将按照您所描述的那样进行排序。

答案 1 :(得分:0)

如果您的数据来自某个类类型的集合,请添加由多个字段组成的自定义属性...

public class YourClassType
{
   public string SomeColumn {get; set;}
   public int SomeInt {get; set; }
   ...

   public string SortCombination { get { return SomeColumn + SomeSort; }}
}

然后在xaml数据网格列中,将“ SortMemberPath”设置为该属性名称。例如:

SortMemberPath =“ SortCombination”;

无需额外的“帮助程序”来进行排序/双排序...它使用一列作为排序基础。您甚至可以设置数字的格式,以确保根据其内容正确地指定相同的长度。