Я создал собственный 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))