C# WPF 中的数据网格 - 1 列默认排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8962923/
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-08-09 05:22:41  来源:igfitidea点击:

Datagrid in WPF - 1 column default sorted

c#wpfsortingdatagrid

提问by Dante1986

In a WPF I have an DataGrid with a few columns.

在 WPF 中,我有一个包含几列的 DataGrid。

At default, there is 1 I want to make it sort to, but I just cant find how I can do this.

默认情况下,我想将其排序为 1,但我无法找到如何执行此操作。

The DataGrid in XAML looks like this:

XAML 中的 DataGrid 如下所示:

<DataGrid x:Name="LibraryView" ItemsSource="{Binding Path=Elements[Persons]}" IsReadOnly="True" LoadingRow="dg_LoadingRow">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Element[Name].Value}" IsReadOnly="True" />
                <DataGridTextColumn Header="Score" Binding="{Binding Path=Element[Score].Value}" IsReadOnly="True" />
                <DataGridTextColumn Header="Date" Binding="{Binding Path=Element[Date].Value}" IsReadOnly="True" />
            </DataGrid.Columns>
        </DataGrid>

And the only code behind it is:

它背后的唯一代码是:

public ScoreBoard()
{
    InitializeComponent();
    DataSet ds = dweMethods.DecryptAndDeserialize("ScoreData.xml");
    XElement TrackList = XElement.Parse(ds.GetXml());
    LibraryView.DataContext = TrackList;
}

What I just can't find is how to make it by default sorted on the "Score" column.

我只是找不到如何使其默认排序在“分数”列上。

Can anyone help me out pointing me in the right direction ?

谁能帮我指出正确的方向?

采纳答案by SomeInternetGuy

NOTE:Using a CollectionViewSource will provide you with more power and control in these situations. When you're learning WPF I recommend understanding how to use CollectionViewSource to solve this problem along with other collection related problems like Groupingand Filtering.

注意:在这些情况下,使用 CollectionViewSource 将为您提供更多的权力和控制。当您学习 WPF 时,我建议您了解如何使用 CollectionViewSource 以及其他与集合相关的问题(例如GroupingFiltering )来解决此问题。

EDIT: This may be due to changes in the specification. This answer is based upon using .NET 4.0, I've not researched whether this solution will work in older versions of the framework.

编辑:这可能是由于规范的变化。此答案基于使用 .NET 4.0,我尚未研究此解决方案是否适用于旧版本的框架。

Given this XAML

鉴于此 XAML

<DataGrid x:Name="LibraryView" ItemsSource="{Binding Path=Elements[Persons]}" IsReadOnly="True" LoadingRow="dg_LoadingRow">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Path=Element[Name].Value}" IsReadOnly="True" />
            <DataGridTextColumn Header="Score" Binding="{Binding Path=Element[Score].Value}" IsReadOnly="True" />
            <DataGridTextColumn Header="Date" Binding="{Binding Path=Element[Date].Value}" IsReadOnly="True" />
        </DataGrid.Columns>
    </DataGrid>

All you need to do is pick a column and specify a sorting direction for that column.

您需要做的就是选择一列并为该列指定排序方向。

<DataGrid x:Name="LibraryView" ItemsSource="{Binding Path=Elements[Persons]}" IsReadOnly="True" LoadingRow="dg_LoadingRow">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Element[Name].Value}" IsReadOnly="True" />
                <DataGridTextColumn Header="Score" Binding="{Binding Path=Element[Score].Value}" IsReadOnly="True" SortDirection="Ascending" />
                <DataGridTextColumn Header="Date" Binding="{Binding Path=Element[Date].Value}" IsReadOnly="True" />
            </DataGrid.Columns>
        </DataGrid>

This will default the sorting to the 2nd column in the ascending direction.

这将默认排序为升序的第二列。

回答by doblak

I described how to sort in code by first of the columns here: Initial DataGrid Sorting

我描述了如何按此处的第一列对代码进行排序:Initial DataGrid Sorting

You could adapt the code to sort by your specific desired column, although the whole approach seems messy.

您可以调整代码以按您特定的所需列进行排序,尽管整个方法看起来很混乱。

If you want to do it in XAML ... what may work is setting CollectionViewSource.SortDescriptions:

如果你想在 XAML 中做到这一点......可能有效的是设置 CollectionViewSource.SortDescriptions:

<CollectionViewSource x:Key="cvs" Source="{StaticResource myItemsSource}">
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="MyPropertyName" Direction="Ascending"/>
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

But I've never tried the latter.

但我从来没有尝试过后者。

回答by Charles Clayton

If you want to do it programatically, you can do so like this:

如果你想以编程方式进行,你可以这样做:

 MyDataGrid.ItemsSource = DataContext.RowItems.OrderBy(p => p.Score).ToList();

回答by Gideon

You can use a ICollectionViewin your code behind. Assuming you have defined ObservableCollection<yourPersonClass> Personsand Namesis a property of yourPersonClass

您可以ICollectionView在后面的代码中使用 a 。假设您已经定义ObservableCollection<yourPersonClass> Persons并且Names是 yourPersonClass 的一个属性

public ICollectionView View; 
View = CollectionViewSource.GetDefaultView(Persons);
View.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
View.Refresh();