にListCollectionView
、新しいを追加するだけGroupDescriptions
です。しかし、グループに基づいてそれらをソートする方法を探していItemCount
ます。
したがって、最初の位置で、アイテムが最も多いグループを取得します。
にListCollectionView
、新しいを追加するだけGroupDescriptions
です。しかし、グループに基づいてそれらをソートする方法を探していItemCount
ます。
したがって、最初の位置で、アイテムが最も多いグループを取得します。
グループを別のビューでラップして並べ替えることができます。
XAML の例:
<CollectionViewSource x:Key="Items2" Source="{Binding Groups,
Source={StaticResource Items}}">
<CollectionViewSource.SortDescriptions>
<cm:SortDescription PropertyName="ItemCount" Direction="Descending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
優れたライブラリ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))