【问题标题】:How to remember the sorted如何记住排序的
【发布时间】:2015-12-12 09:06:18
【问题描述】:

在 WPF DataGrid 的“刷新”功能中,我试图在将项目重新加载到 DataGrid 之前保存排序标准,加载新项目,然后设置以前的排序标准。我正在使用此代码:

   void Refresh(DataGrid docsDataGrid) {
        var sd = ListSortDirection.Ascending;
        DataGridColumn sortCol = null;
        foreach (var column in DocsDataGrid.Columns)
        {
            if (column.SortDirection != null)
            {
                sd = (ListSortDirection)column.SortDirection;
                sortCol = column;
            }
        }

        docsDataGrid.ItemsSource = GetLatestItems();
        sortCol.SortDirection = sd;
   }

通过上面的代码,我确实得到了“排序列”的视觉提示,但是新加载的项目并没有真正排序,所以有点海市蜃楼。我在sortCol.Direction = sd 之后尝试了docsDataGrid.Items.Refresh(),但数据网格没有选择以编程方式设置的排序方向。

如何利用 DataGrid 的内置排序工具并以编程方式设置要排序的列(并实际按该列对行进行排序)?

【问题讨论】:

    标签: c# wpf sorting datagrid


    【解决方案1】:

    经过一些额外的研究,这个 SO Q&A 中的 SortDataGrid 示例帮助了我:Sort a wpf datagrid programmatically

    看来我也应该处理 DataGridItems.SortDescriptions。

    【讨论】:

      【解决方案2】:

      不确定这是否对您有帮助,但为什么不将选定的 SortDirection 保存在 setting 中并在需要时加载它?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-29
        • 1970-01-01
        • 2021-11-16
        • 2016-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多