3

WPF データグリッドでグループ化を実装しています。グループ化されたアイテムを並べ替えたい。たとえば、データグリッドには 4 つの列 (empno、name、dept、address) があります。部門列ごとにグループ化しています。部門の列ヘッダーをクリックすると、グループ化されたアイテムを並べ替えたいと思います。

ここでは、ListCollectionView を使用してコード ビハインド内のアイテムをグループ化しています。

public  ListCollectionView collection;
collection = new ListCollectionView(obj.empData);

collection.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
dgData.Items.SortDescriptions.Add
          (new System.ComponentModel.SortDescription
                   ("Country"
                    ,System.ComponentModel.ListSortDirection.Descending
                   )
           );
dgData.Items.SortDescriptions.Add
          (new System.ComponentModel.SortDescription
                    ("Contact"
                     , System.ComponentModel.ListSortDirection.Descending
                    )
          );
dgData.ItemsSource = collection;

private void dgData_Sorting
        (object sender, Microsoft.Windows.Controls.DataGridSortingEventArgs e)
{
    if (e.Column.SortDirection.ToString() == "Ascending")
    {
        //dgData.Items.SortDescriptions.Clear();
        collection.Refresh();
        collection = new ListCollectionView(obj.empData);
        collection.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
        dgData.Items.SortDescriptions.Add
             ( new System.ComponentModel.SortDescription
                  ("Country"
                   , System.ComponentModel.ListSortDirection.Descending
                  )
              );
         dgData.ItemsSource = collection;
    }
}

並べ替え順序を変更した後、UI に反映されません。これを実装する正しい方法を教えてください。

4

3 に答える 3

3

MSDN の記事How to: Sort a GridView Column When a Header Is Clickedを見たことがありますか?これはListView That Sorts Data Sampleのサンプルを参照しており、後者には ( Download sample ) リンク があります

面白いですが、サンプル ダウンロードへの最終的な参照は、MSDN 記事の .NET 3.0 および 3.5 バージョンでのみ利用できます。.NET 4.0 および 4.5 のバージョンでは利用できませんが、コード スニペットは同じです。

上記の MSDN サンプルに基づくサンプルを含むブログ記事もあります。

実行可能な Visual Studio プロジェクト (WPF Toolkit に依存) に関する MSDN ブログ記事もあります。

于 2013-04-17T08:10:30.700 に答える
1

このコードを使用できます (制限: 「連絡先」でソートすると、「国」の順序は昇順にリセットされます):

void dgData_Sorting(object sender, DataGridSortingEventArgs e)
{
    // reset sortings
    collection.SortDescriptions.Clear();

    // define column sort
    e.Column.SortDirection = e.Column.SortDirection 
          == ListSortDirection.Ascending 
             ? ListSortDirection.Descending : ListSortDirection.Ascending;

    // sort collection
    collection.SortDescriptions.Add
             (new SortDescription
                   (e.Column.SortMemberPath
                    , e.Column.SortDirection.GetValueOrDefault()
                   )
              );

    // mark event as handled otherwise the datagrid will "reset" your custom sorting
    e.Handled = true;
}
于 2013-04-17T12:28:04.077 に答える
0

Live Sorting をオンにすると、2 番目の並べ替え列が実際に有効になることがわかりました。

collection.IsLiveSortingRequested = true;
collection.LiveSortingProperties.Clear();
collection.LiveSortingProperties.Add("Contact");
于 2016-02-03T23:35:57.097 に答える