2012-01-22 10 views
21

WPFでは、私はいくつかの列を持つDataGridを持っています。WPF - 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; 
} 

がデフォルトでソートされたことで、それを作成する方法であります「スコア」列。

誰でも私を正しい方向に向けることができますか?

+0

は、CollectionViewSourceを見てみましょう。 –

+0

私はすでにこれを試しました: 'ICollectionView view = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource); view.SortDescriptions.Clear(); view.SortDescriptions.Add(新しいSortDescription( "LastName"、ListSortDirection.Ascending)); view.Refresh(); 'しかし、これは私の上のコードではうまくいかないようです。私はそれを動作させるために何をすべきか理解していません。 – Dante1986

+0

TrackListの子供を並べ替えることを検討しましたか? –

答えて

42

解決策は、現在の回答よりも簡単です。

注: CollectionViewSource を使用するには、これらの状況でより多くのパワーとコントロールを提供します。 あなたがWPFを学習しているとき、私はがフィルタリングをグループ化するように、他のコレクション 関連の問題と一緒にこの問題を解決するために CollectionViewSourceを使用する方法を理解することをお勧めします。

EDIT:これは仕様の変更によるものです。この回答は.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

全体的なアプローチが厄介と思われるが、あなたは、あなたの特定の目的の列でソートするコードを適応させることができます。

何働くことはCollectionViewSource.SortDescriptionsを設定している...あなたはXAMLでそれをしたい場合:

<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つの欠点があります。それは最初の列をソートしますが(メソッドに記述されています)、どのようにして2番目(または別の)のソートを行うことができますか? – 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(); 
関連する問題