Datagrid in WPF - 1 column default sorted
在 WPF 中,我有一个包含几列的 DataGrid。
默认情况下,我想将其排序为 1 个,但我找不到如何做到这一点。
XAML 中的 DataGrid 如下所示:
1 2 3 4 5 6 7 | <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> |
它背后唯一的代码是:
1 2 3 4 5 6 7 | public ScoreBoard() { InitializeComponent(); DataSet ds = dweMethods.DecryptAndDeserialize("ScoreData.xml"); XElement TrackList = XElement.Parse(ds.GetXml()); LibraryView.DataContext = TrackList; } |
我只是找不到如何让它在"分数"列中默认排序。
谁能帮我指出正确的方向?
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 Grouping and Filtering.
编辑:这可能是由于规范的变化。此答案基于使用 .NET 4.0,我尚未研究此解决方案是否适用于旧版本的框架。
鉴于此 XAML
1 2 3 4 5 6 7 | <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> |
您需要做的就是选择一列并为该列指定排序方向。
1 2 3 4 5 6 7 | <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> |
这将默认按升序排列到第 2 列。
我在这里描述了如何通过第一列对代码进行排序:Initial DataGrid Sorting
您可以调整代码以按特定的所需列进行排序,尽管整个方法看起来很混乱。
如果您想在 XAML 中执行此操作...可能有效的是设置 CollectionViewSource.SortDescriptions:
1 2 3 4 5 | <CollectionViewSource x:Key="cvs" Source="{StaticResource myItemsSource}"> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName="MyPropertyName" Direction="Ascending"/> </CollectionViewSource.SortDescriptions> </CollectionViewSource> |
但我从未尝试过后者。
您可以在后面的代码中使用
假设你已经定义了
的一个属性
1 2 3 4 | public ICollectionView View; View = CollectionViewSource.GetDefaultView(Persons); View.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); View.Refresh(); |
如果你想以编程方式进行,你可以这样做:
1 | MyDataGrid.ItemsSource = DataContext.RowItems.OrderBy(p => p.Score).ToList(); |