3

FirstName、LastName プロパティを含むカスタム エンティティがあります (他のプロパティもあります)。このエンティティをデータグリッドにバインドすると、FullName (LastName, FirstName) が DataGridTemplateColumn として表示されます (MultiBinding と StringFormat を使用)。ユーザーはデータグリッド列を並べ替えることができ、FullName 列をクリックすると、レコードは LastName、次に FirstName で並べ替える必要があります (並べ替えの方向はクリック数に基づいて切り替わります)。上記のシナリオで目的の並べ替え (複数列) を実現できるかどうかを知りたいですか?

SortMemberPath 属性を使用してみましたが、指定できる列は 1 つだけです。

FullName という名前のカスタム ReadOnly プロパティをエンティティに追加すると機能することはわかっていますが、実装された MultiBinding で同じことを達成できるかどうかを理解したいだけです。

ありがとう、パンカイ

4

1 に答える 1

0

助けになった別のスレッドを見つけました。Here で説明されているように、DataGrid.Sorting イベントを使用してデフォルトの並べ替えをオーバーライドできます。その答えは、彼または彼女が DataGrid をオーバーライドすると言っていますが、そうする必要はありません。また、データ ソースとして IList を使用したことを前提としているため、代わりに DataTable/DataView (IBindingList) ソースを前提とした例を次に示します。

    private void dgPeople_Sorting(object sender, DataGridSortingEventArgs e)
    {
        //Assumes you've named your column colFullName in XAML
        if (e.Column == colFullName)
        {
            ListSortDirection direction = (e.Column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending;

            //set the sort order on the column
            e.Column.SortDirection = direction;

            //Prevent the default sorting
            e.Handled = true;

            //Get the static default view that the grid is bound to to redefine its sorting
            BindingListCollectionView cv = (BindingListCollectionView)CollectionViewSource.GetDefaultView(dgPeople.ItemsSource);
            cv.SortDescriptions.Clear();
            cv.SortDescriptions.Add(new SortDescription("FirstName", direction));
            cv.SortDescriptions.Add(new SortDescription("LastName", direction));
            cv.Refresh();
        }
    }

DataView ではなく、ICollectionView (この例では BindingListCollectionView) で並べ替えを実行する必要があるという難しい方法を見つけました。そうしないと、DataView で実行する並べ替えが、ICollectionView で設定された並べ替えによって上書きされます。

このリンクは非常に役に立ちました: http://msdn.microsoft.com/en-us/library/ms752347.aspx#what_are_collection_views

于 2012-05-08T20:55:42.113 に答える