WPF DataGrid:指定默认排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29670322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:05:21  来源:igfitidea点击:

WPF DataGrid: Specify a default sort

c#wpfsortingdatagrid

提问by J4N

I've a UserControl, which basically only contains a DataGrid. In this DataGrid, I've a list of event(Severity - Date - Message). The user controls is bound through the ViewModelLocatorof MVVMLight Toolkit.

我有一个 UserControl,它基本上只包含一个 DataGrid。在这个 DataGrid 中,我有一个事件列表(严重性 - 日期 - 消息)。用户控件通过ViewModelLocatorof绑定MVVMLight Toolkit

I've added two things:

我添加了两件事:

In my UserControl resources:

在我的 UserControl 资源中:

<UserControl.Resources>
    <CollectionViewSource x:Key="SortedEvents" Source="{Binding Events}">
        <CollectionViewSource.SortDescriptions>
            <componentModel:SortDescription PropertyName="EventTime" Direction="Descending"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
</UserControl.Resources>

which is used by the DataGrid:

由 DataGrid 使用:

<DataGrid ItemsSource="{Binding Source={StaticResource SortedEvents}}" AutoGenerateColumns="False" >

I also have the SortedDirectionset on the DataGridTextColumn.SortDirection:

我也SortedDirection有一套DataGridTextColumn.SortDirection

<DataGridTextColumn Binding="{Binding EventTime}"  Header="Time" IsReadOnly="True" SortDirection="Descending"/>

When I check the designer, I see the small arrow showing that the DataGrid is sorted correctly.

当我检查设计器时,我看到小箭头表明 DataGrid 已正确排序。

But when I launch the application, the list is not sorted, the arrow is not here. If I click on the column to sort it, it sorts everything correctly, its just the default value which doesn't seems to work.

但是当我启动应用程序时,列表没有排序,箭头不在这里。如果我单击该列对其进行排序,它会正确排序所有内容,它只是似乎不起作用的默认值。

What am I missing? (This dataGrid/column are not even named, so I cannot try to edit them through something else).

我错过了什么?(这个数据网格/列甚至没有命名,所以我不能尝试通过其他方式编辑它们)。

(Initially I was only having the SortDirectionon the DataGridTextColumn. Same result)

(最初我只SortDirectionDataGridTextColumn. 上有相同的结果)

回答by Daniel Filipov

About the "small arrow" check here: ColumnHeader arrows not reflected when sorting a DataGrid in XAML

关于此处的“小箭头”检查: 在 XAML 中对 DataGrid 进行排序时未反映 ColumnHeader arrows

And for a more complex answer: Pre-sorting a DataGrid in WPF

对于更复杂的答案:Pre-sorting a DataGrid in WPF

I think main part is:

我认为主要部分是:

NOTE: the "scm" namespace prefix maps to System.ComponentModel where the SortDescription class lives.

xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"

注意:“scm”命名空间前缀映射到 SortDescription 类所在的 System.ComponentModel。

xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"

Edit

编辑

DataGrid removes the SortDescriptions and GroupDescriptions whenever its ItemsSource changes. This is necessary because unlike other ItemsControls, DataGrid itself adds SortDescriptions when the user clicks the column header and leaving them as is may crash if they are not compatible with the new ItemsSource.

每当其 ItemsSource 更改时,DataGrid 都会删除 SortDescriptions 和 GroupDescriptions。这是必要的,因为与其他 ItemsControl 不同,DataGrid 本身在用户单击列标题时添加了 SortDescriptions,如果它们与新的 ItemsSource 不兼容,则保持原样可能会崩溃。

protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
    {
        if (SetupSortDescriptions != null && (newValue != null)) 
            SetupSortDescriptions(this, new ValueEventArgs<CollectionView>((CollectionView)newValue)); 

        base.OnItemsSourceChanged(oldValue, newValue);
    }