首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将WPF DataGrid列的默认排序方向设置为降序?

如何将WPF DataGrid列的默认排序方向设置为降序?
EN

Stack Overflow用户
提问于 2016-05-20 21:05:20
回答 1查看 3.7K关注 0票数 7

我有一个带有可排序列的WPF DataGrid。我不想预先排序任何特定列上的网格。当用户第一次单击列标题时,我只想要默认的排序方向是下行而不是升序。

在更改排序列时,SortDescription.Direction上的CollectionViewSource和by DataGridTextColumns的SortDirection属性都不会影响默认排序方向。它总是在列标题的第一次单击时选择升序。

99%的时间它需要下降和切换列在用户工作流中是频繁的,因此这是增加了许多不必要的点击。如果有XAML解决方案,我会非常喜欢,但如果有必要,我会在事件上使用代码欺骗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-20 21:52:35

似乎在不对排序处理程序进行小干预的情况下,您无法做到这一点,因为DataGrid所做的默认排序是这样启动的:

代码语言:javascript
运行
复制
ListSortDirection direction = ListSortDirection.Ascending;
ListSortDirection? sortDirection = column.SortDirection;
if (sortDirection.HasValue && sortDirection.Value == ListSortDirection.Ascending)
    direction = ListSortDirection.Descending;

因此,只有在列被排序之前,并且这类列是上升的情况下,它才会将其翻转到降序。然而,通过微小的黑客,你可以实现你想要的。首先订阅DataGrid.Sorting事件,然后在那里:

代码语言:javascript
运行
复制
private void OnSorting(object sender, DataGridSortingEventArgs e) {
    if (e.Column.SortDirection == null)
         e.Column.SortDirection = ListSortDirection.Ascending;
    e.Handled = false;
}

因此,基本上,如果还没有排序--您可以将其切换到Ascending,并将其传递给DataGrid的默认排序(通过将e.Handled设置为false)。在排序开始时,它会为您将其切换到Descending,这就是您想要的。

您可以在xaml中使用附加属性来完成这一任务,如下所示:

代码语言:javascript
运行
复制
public static class DataGridExtensions {        
    public static readonly DependencyProperty SortDescProperty = DependencyProperty.RegisterAttached(
        "SortDesc", typeof (bool), typeof (DataGridExtensions), new PropertyMetadata(false, OnSortDescChanged));

    private static void OnSortDescChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        var grid = d as DataGrid;
        if (grid != null) {
            grid.Sorting += (source, args) => {
                if (args.Column.SortDirection == null) {
                    // here we check an attached property value of target column
                    var sortDesc = (bool) args.Column.GetValue(DataGridExtensions.SortDescProperty);
                    if (sortDesc) {
                        args.Column.SortDirection = ListSortDirection.Ascending;
                    }
                }
            };
        }
    }

    public static void SetSortDesc(DependencyObject element, bool value) {
        element.SetValue(SortDescProperty, value);
    }

    public static bool GetSortDesc(DependencyObject element) {
        return (bool) element.GetValue(SortDescProperty);
    }
}

然后在你的xaml中:

代码语言:javascript
运行
复制
<DataGrid x:Name="dg" AutoGenerateColumns="False" ItemsSource="{Binding Items}" local:DataGridExtensions.SortDesc="True">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Value}"
                            Header="Value"
                            local:DataGridExtensions.SortDesc="True" />
    </DataGrid.Columns>
</DataGrid>

因此,基本上您使用SortDesc=true标记SortDesc=true本身,以订阅排序事件,然后只标记需要排序的列。如果存在逻辑,还可以将SortDesc绑定到模型。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37355830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档