簡體   English   中英

WPF中的Datagrid - 1列默認排序

[英]Datagrid in WPF - 1 column default sorted

在WPF中,我有一個包含幾列的DataGrid。

默認情況下,有1我想讓它排序,但我不知道如何做到這一點。

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>

它背后唯一的代碼是:

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

我找不到的是默認情況下如何按“分數”列進行排序。

任何人都可以幫我指出正確的方向嗎?

注意:在這些情況下,使用CollectionViewSource將為您提供更多功能和控制。 當您學習WPF時,我建議您了解如何使用CollectionViewSource解決此問題以及其他與集合相關的問題,例如分組過濾

編輯:這可能是由於規范的變化。 這個答案是基於使用.NET 4.0,我沒有研究這個解決方案是否適用於舊版本的框架。

鑒於此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>

您需要做的就是選擇一列並指定該列的排序方向。

<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>

這將默認按向上方向排序到第二列。

我在下面的第一列描述了如何對代碼進行排序: 初始DataGrid排序

您可以調整代碼以按特定的所需列進行排序,盡管整個方法看起來很混亂。

如果你想在XAML中做...可能有用的是設置CollectionViewSource.SortDescriptions:

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

但我從未嘗試過后者。

如果你想以編程方式進行,你可以這樣做:

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

您可以在后面的代碼中使用ICollectionView 假設您已定義ObservableCollection<yourPersonClass> PersonsNames是yourPersonClass的屬性

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM