2
在ListCollectionView
上,我只是添加新的GroupDescriptions
。 但我正在尋找一種方法來根據組的ItemCount
對它們進行排序。根據組別ItemCount排序羣組
所以在第一個位置,我會得到最多的項目組。
在ListCollectionView
上,我只是添加新的GroupDescriptions
。 但我正在尋找一種方法來根據組的ItemCount
對它們進行排序。根據組別ItemCount排序羣組
所以在第一個位置,我會得到最多的項目組。
你可以將這些組包裝在另一個視圖中並對其進行排序。
XAML例如:
<CollectionViewSource x:Key="Items2" Source="{Binding Groups,
Source={StaticResource Items}}">
<CollectionViewSource.SortDescriptions>
<cm:SortDescription PropertyName="ItemCount" Direction="Descending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
我創建了一個自定義的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))
您好,感謝您的建議,但即時通訊尋找更清潔的解決方案,可能會隱藏代碼? – myCollections 2012-01-05 07:55:41
@myCollections:代碼背後如何更清潔?我認爲這種方法*是乾淨的,如果你真的想要,它可以很容易地轉換成代碼。 – 2012-01-05 12:23:06