Silverlight:DataGrid像在ItemsControl中分组一样 [英] Silverlight: Datagrid like grouping in ItemsControl
问题描述
是否可以在Silverlight中对ItemsControl或Listbox中的项目进行分组?这些控件绑定到DomainDataSource.
Is it possible to group items in a ItemsControl or Listbox in Silverlight? These controls are bound to a DomainDataSource.
还是有任何第三方控制可以做到这一点?
Or are there any 3rd party controls that do this?
更新:
这是我要创建的UI.
推荐答案
您可以通过使用绑定到
You can do this by using nested ItemsControls
bound to a PagedCollectionView.
说我有一个数据源-MyItems
-带有字段:Category
,Section
和Option
.我可以从IEnumerable(of MyItems)
创建一个PagedCollectionView
,并告诉它要分组的字段.
Say I have a datasource - MyItems
- with fields: Category
, Section
and Option
. I can create a PagedCollectionView
from an IEnumerable(of MyItems)
and tell it which fields to group by.
Dim original As IEnumerable(Of MyItems) = GetMyItems()
Dim pcv = New PagedCollectionView(original)
pcv.GroupDescriptions.Add(New PropertyGroupDescription("Category"))
pcv.GroupDescriptions.Add(New PropertyGroupDescription("Section"))
然后将我的第一个ItemsControl
绑定到PagedCollectionView
Then I bind my first ItemsControl
to the PagedCollectionView
hisMyItems.ItemsSource = pcv.Groups
PCV
创建一个嵌套的层次结构,例如:
The PCV
creates a nested hierachy like:
-Name
-Items
其中,Name
是分组字段中的值,而Items
包含该分组中的行/对象.我想如果您愿意的话,您也可以用xaml创建PCV.
where Name
is the value in the grouped field and Items
contains the rows/objects in that grouping. I guess you could also create the PCV in xaml if you prefer.
xaml类似于:
<controls:HeaderedItemsControl x:Name="hisMyItems" Header="{Binding Name}" ItemsSource="{Binding Items}" >
<controls:HeaderedItemsControl.ItemTemplate>
<DataTemplate>
<controls:HeaderedItemsControl Header="{Binding Name}" ItemsSource="{Binding Items}" ItemsPanel="{StaticResource ItemsPanelTemplate1}" >
<controls:HeaderedItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Option}" />
</DataTemplate>
</controls:HeaderedItemsControl.ItemTemplate>
</controls:HeaderedItemsControl>
</DataTemplate>
</controls:HeaderedItemsControl.ItemTemplate>
</controls:HeaderedItemsControl>
我希望这是有道理的.我试图简化实际应用程序中的内容,但是在复制过程中可能会犯一些错误.显然,您也可以使用常规的ItemsControl或其他控件,并使用模板等进行自定义.
I hope that makes sense. I have tried to simplify things from my actual app but I could have made some mistakes in copying it over. Obviously you could use normal ItemsControls or other controls too and customize with templates etc.
这篇关于Silverlight:DataGrid像在ItemsControl中分组一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!