2012-01-22 77 views
21

在WPF中,我有一个DataGrid有几列。WPF中的Datagrid - 1列默认排序

默认情况下,有1我想使它排序,但我只是无法找到我可以做到这一点。

数据网格中的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> 

和它背后的唯一代码:

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

我只是找不到什么是如何让它默认排序上“分数”栏。

任何人都可以帮助我指出我在正确的方向吗?

+0

看看CollectionViewSource。 – 2012-01-22 20:46:50

+0

我已经试过这个:'ICollectionView view = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource); view.SortDescriptions.Clear(); view.SortDescriptions.Add(new SortDescription(“LastName”,ListSortDirection.Ascending)); view.Refresh();'但是,这似乎并没有与我的上述代码工作,我不知道,也不明白我应该做些什么来使它工作 – Dante1986

+0

你认为排序你的TrackList儿童? – 2012-01-22 22:31:00

回答

42

此解决方案比当前答案提供的更简单。

注:使用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> 

这会默认排序到第2列的上升方向。

8

编辑:请参阅下面的James LaPenn的回答,它应该是可接受的解决方案。


我介绍了如何在代码通过先在这里列的排序:Initial DataGrid Sorting

你能适应的代码由您的具体需要的列进行排序,虽然整个做法似乎凌乱。

如果你想这样做在XAML ...什么可能的工作是设定CollectionViewSource.SortDescriptions:

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

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

+0

这个链接你给(http://stackoverflow.com/questions/8944822/initial-datagrid-sorting/8946420#8946420)似乎确实工作,但目前只有1个缺点。它排序的第一列(在方法中说明),但我怎么能做到它排序第二(或另一个)? – Dante1986

+0

现在通过改变Columns.First();列[1]; (thanx!) – Dante1986

+1

您应该提及需要的命名空间 'xmlns:scm =“clr-namespace:System.ComponentModel; assembly = WindowsBase”' – kskyriacou

2

如果你想以编程方式做到这一点,你可以这样做是这样的:

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