【问题标题】:Sorting groups based on Group ItemCount根据 Group ItemCount 对组进行排序
【发布时间】:2012-01-02 15:16:58
【问题描述】:

ListCollectionView 上,我只需添加新的GroupDescriptions。 但我正在寻找一种基于组的ItemCount 对它们进行排序的方法。

所以在第一个位置,我会得到项目最多的组。

【问题讨论】:

    标签: wpf sorting grouping listcollectionview


    【解决方案1】:

    您可以将组包装在另一个视图中并对其进行排序。

    XAML 示例:

    <CollectionViewSource x:Key="Items2" Source="{Binding Groups,
                                                          Source={StaticResource Items}}">
        <CollectionViewSource.SortDescriptions>
            <cm:SortDescription PropertyName="ItemCount" Direction="Descending"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
    

    【讨论】:

    • 您好,感谢您的建议,但我正在寻找更清洁的解决方案,可能在代码隐藏中?
    • @myCollections:后面的代码究竟如何更简洁?我个人认为这种方法干净的,如果你真的想的话,它可以很容易地翻译成代码......
    【解决方案2】:

    我创建了一个自定义 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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-01
      • 2011-04-05
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      • 2020-12-15
      • 2015-08-21
      • 1970-01-01
      相关资源
      最近更新 更多