简体   繁体   English

WPF 使用重置对 DataGrid 进行排序

[英]WPF sorting DataGrid with reset

I have custom sort for my DataGrid我的 DataGrid 有自定义排序

if (sortPropertyName == "Ip")
{   
     IComparer comparer = null;                    
     e.Handled = true;
     ListSortDirection direction = (e.Column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending;         
     e.Column.SortDirection = direction;         
     ListCollectionView lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(dataGrid.ItemsSource);                     
     comparer = new SortIPAddress(direction);                   
     lcv.CustomSort = comparer;
}

It works well.它运作良好。 And I have method reset sort for other column我有其他列的方法重置排序

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;
         // remove the sort description
         dataGrid.Items.SortDescriptions.RemoveAt(index);
         dataGrid.Items.Refresh();

         if ((Keyboard.Modifiers & ModifierKeys.Shift) != ModifierKeys.Shift)
         {
           // clear any other sort descriptions for the multisorting case
           dataGrid.Items.SortDescriptions.Clear();
           dataGrid.Items.Refresh();
         }

         // stop the default sort
         e.Handled = true;
     }
}

How Can I do reset like in column Ip?我怎样才能像 Ip 列那样重置?

I did:我做了:

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 Ip resets now.排序 Ip 现在重置。 But double sorting with shift is incorrect.但是使用shift双重排序是不正确的。 How to fix?怎么修?

You can clear the sorting of a column by setting the SortDirection property to null :您可以通过将SortDirection属性设置为null来清除列的排序:

e.Column.SortDirection = null;

If you want to remove your custom sorting, you could set the CustomSort property to null :如果要删除自定义排序,可以将CustomSort属性设置为null

ListCollectionView lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(dg.ItemsSource);
lcv.CustomSort = null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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