WPF двойная сортировка в DataGrid - PullRequest
0 голосов
/ 15 января 2019

У меня есть пользовательская сортировка для столбца 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 голосов
/ 15 января 2019

Если ваши данные поступают из коллекции некоторого типа класса, добавьте пользовательское свойство, которое представляет собой комбинацию нескольких полей ...

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

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

Затем в столбце сетки данных xaml установите для «SortMemberPath» имя этого свойства. Пример:

SortMemberPath = "SortCombination";

Нет дополнительного "помощника" для сортировки / двойной сортировки ... он использует один столбец в качестве основы для сортировки. Вы даже можете отформатировать число, чтобы оно было правильно выровнено в зависимости от его содержания.

0 голосов
/ 15 января 2019

Не могли бы вы изменить свой код, добавив группы сортировки, например,

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

, которая применила бы сортировку, как вы описали.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...