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 に反映されません。これを実装する正しい方法を教えてください。