2011-06-24 4 views
14

複数の列を並べ替えるようにWPFデータグリッドを設定するにはどうすればよいですか?2つの並べ替え可能な列を持ち、1番目の列のヘッダーをクリックしてから、セカンダリソート用の2番目の列。ユーザーが最初の列のヘッダーをクリックしたときに、複数の列の並べ替えが自動的に行われるようにしたいと思います。これをxamlで完全に行う方法はありますか?後ろのコードでどうやったらいい?現在VB.Netを使用していますが、C#スニペットがあればそれを使用できます。ありがとう!WPFデータグリッドの複数の列を並べ替える

答えて

17

あなたはこのようSystem.ComponentModel名前空間を追加することによってこれを行うことができます。

xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" 

が、その後CollectionViewSource XAML内でこのように新しいSortDescriptionsにを追加します。

<CollectionViewSource … > 
      <CollectionViewSource.SortDescriptions> 
       <scm:SortDescription PropertyName="Column1"/> 
       <scm:SortDescription PropertyName="Column2"/> 
      </CollectionViewSource.SortDescriptions> 
</CollectionViewSource> 

この意志列1、列2のデータグリッドをソートします。

は編集:

も背後に、この使用してC#のコードを実行するには非常に簡単です:

private void btnSort_Click(object sender, RoutedEventArgs e) 
    { 
     System.Windows.Data.CollectionViewSource myViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("The_ViewSource_Name"))); 
     myViewSource.SortDescriptions.Add(new SortDescription("Column1", ListSortDirection.Ascending)); 
     myViewSource.SortDescriptions.Add(new SortDescription("Column2", ListSortDirection.Ascending)); 
    } 

EDIT2:回避策は、列ヘッダーの左マウスクリックをキャッチするために行うことができる

グリッドが次のようにその列でソートされないようにします。

  • 無効にグリッドという名前のプロパティ CanUserSortColumns

enter image description here

  • グリッド のPreviewMouseLeftButtonUpイベントにこのコードを追加します。

    private void myDataGrid_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
        DependencyObject dep = (DependencyObject)e.OriginalSource; 
        while ((dep != null) && 
        !(dep is DataGridCell) && 
        !(dep is DataGridColumnHeader)) 
        { 
         dep = VisualTreeHelper.GetParent(dep); 
        } 
    
        if (dep == null) 
         return; 
    
        if (dep is DataGridColumnHeader) 
        { 
         DataGridColumnHeader columnHeader = dep as DataGridColumnHeader; 
         // check if this is the wanted column 
         if (columnHeader.Column.Header.ToString() == "The_Wanted_Column_Title") 
         { 
          System.Windows.Data.CollectionViewSource myViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("myViewSource"))); 
          myViewSource.SortDescriptions.Clear(); 
          myViewSource.SortDescriptions.Add(new SortDescription("Column1", ListSortDirection.Ascending)); 
          myViewSource.SortDescriptions.Add(new SortDescription("Column2", ListSortDirection.Ascending)); 
         } 
         else 
         { 
          //usort the grid on clicking on any other columns, or maybe do another sort combination 
          System.Windows.Data.CollectionViewSource myViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("myViewSource"))); 
          myViewSource.SortDescriptions.Clear(); 
         } 
    
        } 
    } 
    

このコードを変更して展開し、要件を満たすことができます。

+0

OPは、複数の列見出しをクリックして、ユーザーがグリッドを並べ替える方法を尋ねました。プログラムで基本的なコレクションを一度ソートする方法ではありません。 –

+0

@djacobson私は質問をうまく読んだと思う。彼は次のように書いている。「xamlでこれを完全に行う方法はありますか?」 –

+0

@djacobsonさらに、複数の列見出しをクリックすると、グリッドは自動的に選択した列を並べ替えます。 –

11

これが他の人に役立つことを願っています。私のソリューションは、デフォルトの並べ替え機能を維持し、複数の列の並べ替えを許可します。

あなたのデータグリッドに

<DataGrid x:Name="dataGridName" Sorting="dataGridName_Sorting"> 

を並べ替えイベントを入れて、今

private void dataGridName_Sorting(object sender, DataGridSortingEventArgs e) 
{ 
    var dgSender = (DataGrid) sender; 
    var cView = CollectionViewSource.GetDefaultView(dgSender.ItemsSource); 

    //Alternate between ascending/descending if the same column is clicked 
    ListSortDirection direction = ListSortDirection.Ascending; 
    if (cView.SortDescriptions.FirstOrDefault().PropertyName == e.Column.SortMemberPath) 
     direction = cView.SortDescriptions.FirstOrDefault().Direction == ListSortDirection.Descending ? ListSortDirection.Ascending : ListSortDirection.Descending; 

    cView.SortDescriptions.Clear(); 
    AddSortColumn((DataGrid)sender, e.Column.SortMemberPath, direction); 
    //To this point the default sort functionality is implemented 

    //Now check the wanted columns and add multiple sort 
    if (e.Column.SortMemberPath == "WantedColumn") 
    { 
     AddSortColumn((DataGrid)sender, "SecondColumn", direction); 
    } 
    e.Handled = true; 
} 

private void AddSortColumn(DataGrid sender, string sortColumn, ListSortDirection direction) 
{ 
    var cView = CollectionViewSource.GetDefaultView(sender.ItemsSource); 
    cView.SortDescriptions.Add(new SortDescription(sortColumn, direction)); 
    //Add the sort arrow on the DataGridColumn 
    foreach (var col in sender.Columns.Where(x => x.SortMemberPath == sortColumn)) 
    { 
     col.SortDirection = direction; 
    } 
} 
の後ろにコード

でのDataGridColumnのsortDirectionは、グリッド上の矢印を示すことができます。

関連する問題