2016-08-04 68 views
0

我有一个WPF项目 - 一个有四列的数据网格:ColumnOne,ColumnTwo,columnThree,ColumnFour。是否有可能当用户按ColumnOne或ColumnTwo排序时 - 那么后面的代码也会通过ColumnThree添加排序,因此它会像SortBy(“ColumnOne”)。ThenBy(“ColumnThree”)那样排序。如果这很重要,我的DataGrid的ItemsSource是PagedCollectionView,它支持SortDescriptors。DataGrid附加另一列的排序

回答

1

您必须覆盖DataGrid.OnSorting就像在这个简单的例子(但是请延长到您要求),并在您的XAML使用定制控制而不是标准DataGrid的。

public class MyDataGrid : DataGrid 
    { 
     protected override void OnSorting(DataGridSortingEventArgs eventArgs) 
     { 
      base.OnSorting(eventArgs); 
      var test = eventArgs.Column; 
      if (test.Header.ToString() == "ColumnOne" && test.SortDirection.HasValue 
       && test.SortDirection.Value.Equals(ListSortDirection.Ascending) 
       ) 
      { 
       ICollectionView view = CollectionViewSource.GetDefaultView(this.ItemsSource); 
       view.SortDescriptions.Add(new SortDescription("ColumnThree", ListSortDirection.Ascending)); 
       view.Refresh(); 

       this.Columns[2].SortDirection = ListSortDirection.Ascending; 
      } 
     } 
    } 

上面代码处理两者总汇排序和只在一个情况下,SortDirection属性设置为ColumnThree:当由ColumnOne升序用户订单。