簡體   English   中英

WPF DataGrid中的雙重排序

[英]WPF double sorting in DataGrid

我對帶有重置的IP列進行了自定義排序。 我已經重置其他列

在此處輸入圖片說明

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列重置進行排序。如何解決雙重排序? 論壇要求更多詳細信息,但我不知道還要添加什么

您可以修改代碼以添加排序組嗎?

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

這將按照您所描述的那樣進行排序。

如果您的數據來自某個類類型的集合,則添加一個自定義屬性,該屬性是多個字段的組合...

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

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

然后,在xaml數據網格列中,將“ SortMemberPath”設置為該屬性名稱。 例如:

SortMemberPath =“ SortCombination”;

無需額外的“幫助程序”來進行排序/雙排序...它使用一列作為排序基礎。 您甚至可以設置數字格式,以確保根據其內容正確地將其長度設置為正確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM