2

在 a 上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

我创建了一个自定义 ListCollectionView 以使顶级组排序成为可能,使用了一个很棒的库System.Linq.Dynamic

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 回答