2

ListCollectionView、新しいを追加するだけGroupDescriptionsです。しかし、グループに基づいてそれらをソートする方法を探していItemCountます。

したがって、最初の位置で、アイテムが最も多いグループを取得します。

4

2 に答える 2

3

グループを別のビューでラップして並べ替えることができます。

XAML の例:

<CollectionViewSource x:Key="Items2" Source="{Binding Groups,
                                                      Source={StaticResource Items}}">
    <CollectionViewSource.SortDescriptions>
        <cm:SortDescription PropertyName="ItemCount" Direction="Descending"/>
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>
于 2012-01-02T15:26:51.933 に答える
0

優れたライブラリSystem.Linq.Dynamicを使用して、トップレベルのグループの並べ替えを可能にするカスタム ListCollectionView を作成しました。

Public Class ListCollectionViewEx
    Inherits ListCollectionView
    Private _groupSortDescriptions As SortDescriptionCollection
    Private _groups As ReadOnlyObservableCollection(Of Object)

    Public Overridable ReadOnly Property GroupSortDescriptions As SortDescriptionCollection
        Get
            Return Me._groupSortDescriptions
        End Get
    End Property

    Public Overrides ReadOnly Property Groups As System.Collections.ObjectModel.ReadOnlyObservableCollection(Of Object)
        Get
            If Me._groupSortDescriptions.Count > 0 Then
                If Me._groups Is Nothing AndAlso
                    MyBase.Groups IsNot Nothing Then
                    Dim qs As String = String.Join(
                        ",",
                        Me._groupSortDescriptions.
                        Select(Function(gsd) String.Format("{0} {1}", gsd.PropertyName, If(gsd.Direction = ListSortDirection.Ascending, "ASC", "DESC"))).
                        ToArray)

                    Dim sortedGroups = MyBase.Groups.
                        Select(Function(g) DirectCast(g, CollectionViewGroup)).
                        AsQueryable.OrderBy(qs).
                        AsEnumerable

                    If sortedGroups IsNot Nothing AndAlso
                        sortedGroups.Count > 0 Then
                        Me._groups = New ReadOnlyObservableCollection(Of Object)(New ObservableCollection(Of Object)(sortedGroups))
                    End If
                End If

                Return Me._groups
            End If

            Return MyBase.Groups
        End Get
    End Property

    Public Sub New(collection As IEnumerable)
        MyBase.New(collection)
        Me._groupSortDescriptions = New SortDescriptionCollection()
    End Sub
End Class

使用法:

View.GroupSortDescriptions.Add(New SortDescription("ItemCount", ListSortDirection.Descending))
于 2013-10-30T11:25:30.227 に答える