Silverlight 以编程方式折叠所有DataGrid组,而不使用PagedCollectionView

Silverlight 以编程方式折叠所有DataGrid组,而不使用PagedCollectionView,silverlight,silverlight-4.0,datagrid,domaindatasource,Silverlight,Silverlight 4.0,Datagrid,Domaindatasource,我正在尝试在Xaml中尽可能多地执行操作,而不是为一个相对简单的应用程序编写代码。我将DataGrid绑定到Silverlight 4中的DomainDataSource,并将DomainDataSource的GroupDescriptor绑定到组合框,允许用户根据选择的值对DataGrid中的行进行分组。我想让他们能够单击按钮折叠/展开所有组。我知道这可以使用PagedCollectionView来完成,但我最终还是在代码隐藏中完成了分组等工作。有没有一种不使用PagedCollectionV

我正在尝试在Xaml中尽可能多地执行操作,而不是为一个相对简单的应用程序编写代码。我将DataGrid绑定到Silverlight 4中的DomainDataSource,并将DomainDataSource的GroupDescriptor绑定到组合框,允许用户根据选择的值对DataGrid中的行进行分组。我想让他们能够单击按钮折叠/展开所有组。我知道这可以使用PagedCollectionView来完成,但我最终还是在代码隐藏中完成了分组等工作。有没有一种不使用PagedCollectionView的方法来实现这一点


我知道DataGrid.CollapseRowGroupCollectionViewGroup collectionViewGroup,bool collapseAllSubgroups方法,但我还没有找到一种方法来迭代顶级组

这是我想到的。它提供了扩展或折叠所有级别或特定级别的灵活性。如果需要,可以对其进行重构以删除重复的代码。要在单个调用中展开或折叠所有级别的所有组,只需为groupingLevel参数传入0,为collapseAllSublevels参数传入true。通过使用HashSet,可以自动从groups集合中消除重复项

    /// <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);
    }

我在MSDN文档中找到以下注释:CollectionViewGroup表示由PagedCollectionView对象根据其GroupDescriptions创建的组。在这种情况下,除非您使用的是PagedCollectionView,否则可能无法使用DataGrid.CollapseRowGroup方法。还有别的办法吗?在没有PCV的帮助下,您可以从DDS中定义可以在数据网格中展开和折叠的组,这似乎很奇怪,但是您似乎无法在没有PCV的情况下通过编程控制这些组。