【问题标题】:DataGrid append another column sortDataGrid 追加另一列排序
【发布时间】:2016-08-04 08:36:38
【问题描述】:

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

【问题讨论】:

    标签: wpf


    【解决方案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;
                }
            }
        }
    

    上面的代码只在一种情况下处理集合排序和ColumnThreeSortDirection 属性设置:当用户按ColumnOne 升序排序时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-11
      • 2018-10-23
      • 2021-02-16
      • 2014-07-16
      • 2020-07-03
      • 2012-11-04
      • 1970-01-01
      • 2021-12-20
      相关资源
      最近更新 更多