2

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.

MylesRip
  • 1,212
  • 17
  • 29
  • I found the following comment in the MSDN documentation: "(CollectionViewGroup) represents a group created by a PagedCollectionView object based on its GroupDescriptions." In that case, it seems it may not be possible to use the DataGrid.CollapseRowGroup method unless you are using a PagedCollectionView. **Is there another way?** It seems really odd that you can define groups (which can be expanded and collapsed) in a DataGrid from the DDS *without* the help of a PCV, but you can't seem to control these groups programatically without a PCV. – MylesRip May 04 '11 at 20:15

1 Answers1

0

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.

    /// <summary>
    /// Collapse all groups at a specific grouping level.
    /// </summary>
    /// <param name="groupingLevel">The grouping level to collapse. Level 0 is the top level. Level 1 is the next level, etc.</param>
    /// <param name="collapseAllSublevels">Indicates whether levels below the specified level should also be collapsed. The default is "false".</param>
    private void CollapseGroups(int groupingLevel, bool collapseAllSublevels = false)
    {
        if (myGrid.ItemsSource == null)
            return;
        HashSet<CollectionViewGroup> groups = new HashSet<CollectionViewGroup>();
        foreach (object item in myGrid.ItemsSource)
            groups.Add(myGrid.GetGroupFromItem(item, groupingLevel));
        foreach (CollectionViewGroup group in groups)
            myGrid.CollapseRowGroup(group, collapseAllSublevels);
    }

    /// <summary>
    /// Expand all groups at a specific grouping level.
    /// </summary>
    /// <param name="groupingLevel">The grouping level to expand. Level 0 is the top level. Level 1 is the next level, etc.</param>
    /// <param name="expandAllSublevels">Indicates whether levels below the specified level should also be expanded. The default is "false".</param>
    private void ExpandGroups(int groupingLevel, bool expandAllSublevels = false)
    {
        if (myGrid.ItemsSource == null)
            return;
        HashSet<CollectionViewGroup> groups = new HashSet<CollectionViewGroup>();
        foreach (object item in myGrid.ItemsSource)
            groups.Add(myGrid.GetGroupFromItem(item, groupingLevel));
        foreach (CollectionViewGroup group in groups)
            myGrid.ExpandRowGroup(group, expandAllSublevels);
    }
MylesRip
  • 1,212
  • 17
  • 29