WPFでのDataGridの事前ソート
-
06-07-2019 - |
質問
WPFアプリには、Name列を含むいくつかの列を持つ DataGrid
があります。ユーザーが特定のビューに切り替えた場合、データを名前で事前に並べ替える必要があります(そして、ユーザーがそのヘッダーをクリックしたかのように、名前ヘッダーに並べ替え矢印を表示したい)。ただし、これを実現するための期待されるプロパティが見つかりません。 SortColumn
、 SortColumnIndex
、 SortDirection
などのようなものを探していました。
デフォルトのソート列と方向をマークアップ(XAML)で指定することは可能ですか、それともWPF Toolkit DataGrid
でサポートされていませんか?
解決
WPF Toolkit DataGridコントロールについて話していると仮定すると、 CanUserSortColumnsプロパティをtrueに設定し、 DataGridの各DataGridColumnのSortMemberPathプロパティ。
最初にコレクションを並べ替える限り、CollectionViewSourceを使用して並べ替えを設定し、DataGridのItemsSourceとして割り当てる必要があります。 XAMLでこれを行う場合、次のように簡単です:
<Window.Resources>
<CollectionViewSource x:Key="MyItemsViewSource" Source="{Binding MyItems}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="MyPropertyName"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
<DataGrid ItemsSource="{StaticResource MyItemsViewSource}">
</DataGrid>
注:&quot; scm&quot;名前空間プレフィックスは、SortDescriptionクラスが存在するSystem.ComponentModelにマップされます。
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
編集:この投稿から多くの人が助けを得たと思うので、この投票されたコメントをこの回答に含める必要があります:
これを使用して動作させる必要がありました:
<DataGrid ItemsSource="{Binding Source={StaticResource MyItemsViewSource}}">
他のヒント
これは古い投稿であることを知っていますが、Drew Marshの回答に加えて、列ヘッダーの矢印が表示されないというDanMの問題への対応として... SortDirectionプロパティをDataGridColumnに追加する必要があります。
<DataGridTextColumn Header="Name" Binding="{Binding Name}" SortDirection="Ascending" />
これについて質問を投稿し、数日後に答えを見つけました:
ItemsSourceがCollectionViewSource例外をサポートしていない場合、DataGridのDataContextを「MyItemsViewSource」として、ItemsSourceを{Binding}として次のように設定できます。
<DataGrid DataContext="{StaticResource MyItemsViewSource}" ItemsSource="{Binding}">
</DataGrid>
ItemsSourceがCollectionViewSource
例外をサポートしていない場合、コレクションをDataGridに参照する前にLinqで並べ替えることができます。
ObservableCollection<MyDataClass> myCollection = new ObservableCollection<MyDataClass>();
dataGrid.ItemsSource = from item in myCollection orderby item select item;
MyDataClass
への IComparable
インターフェースを実装する必要があります:
public class MyDataClass : IComparable<MyDataClass> {
public int CompareTo(Classified other) {
return other.Value.CompareTo(this.Value); // DESC
return this.Value.CompareTo(other.Value); // ASC
}
}
これは私には有効です。
ListSortDirection sortDirection;
int selectedColumnIndex;
private void customerDataGrid_Sorting(object sender, DataGridSortingEventArgs e)
{
selectedColumnIndex = e.Column.DisplayIndex;
sortDirection = (e.Column.SortDirection == ListSortDirection.Ascending ? ListSortDirection.Descending: ListSortDirection.Ascending);
}
private void applySortDescriptions(ListSortDirection listSortDirection)
{
//Clear current sort descriptions
customerDataGrid.Items.SortDescriptions.Clear();
//Get property name to apply sort based on desired column
string propertyName = customerDataGrid.Columns[selectedColumnIndex].SortMemberPath;
//Add the new sort description
customerDataGrid.Items.SortDescriptions.Add(new SortDescription(propertyName, listSortDirection));
//apply sort
applySortDirection(listSortDirection);
//refresh items to display sort
customerDataGrid.Items.Refresh();
}
private void applySortDirection(ListSortDirection listSortDirection)
{
customerDataGrid.Columns[selectedColumnIndex].SortDirection = listSortDirection;
}