Silverlight: Datagrid like grouping in ItemsControl

被刻印的时光 ゝ 提交于 2019-12-12 10:37:34

问题


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?

UPDATE:

This is the sort of UI I am trying to create.


回答1:


You can do this by using nested ItemsControls bound to a 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"))

Then I bind my first ItemsControl to the PagedCollectionView

hisMyItems.ItemsSource = pcv.Groups

The PCV creates a nested hierachy like:

-Name
    -Items

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.

The xaml would look something like:

<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>

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.




回答2:


The DataGrid control supports grouping.

Tim Heuer has a good blog on grouping with a datagrid. link text




回答3:


Perhaps the control you are really looking for is the Accordian Control from the Toolkit.

See sample of Accordian behaviour here.

Note that the actual appearance is as style-able as any other control. The basic function is to group categories of items that would otherwise be a straight-forward List.




回答4:


Check the solution from David Ans http://blogs.msdn.com/b/delay/archive/2010/03/17/confessions-of-a-listbox-groupie-using-ivalueconverter-to-create-a-grouped-list-of-items-simply-and-flexibly.aspx



来源:https://stackoverflow.com/questions/2019942/silverlight-datagrid-like-grouping-in-itemscontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!