I’m trying to do as much as possible in the Xaml (rather than code-behind) for a relatively simple application. I have the DataGrid bound to a DomainDataSource in Silverlight 4 and I’m binding the DomainDataSource’s GroupDescriptors to ComboBoxes allowing the user to group the rows in the DataGrid according to values they select. I’d like to give them the ability to click a button to collapse/expand all groups. I know this can be done using a PagedCollectionView, but then I end up doing the grouping, etc. in code-behind. Is there a way to accomplish this without using a PagedCollectionView?
I’m aware of the DataGrid.CollapseRowGroup(CollectionViewGroup collectionViewGroup, bool collapseAllSubgroups) method, but I haven’t found a way to iterate through the top-level groups.
Here’s what I came up with. It provides flexibility to expand or collapse all levels or specific levels. (This could be refactored to remove duplicate code, if desired.) To expand or collapse all groups at all levels in a single call, simply pass in “0” for the groupingLevel parameter and “true” for the collapseAllSublevels parameter. By using a HashSet, duplicates are automatically eliminated from the “groups” collection.