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

发布于 2024-11-04 19:05:15 字数 435 浏览 0 评论 0原文

我尝试在 Xaml(而不是代码隐藏)中为相对简单的应用程序做尽可能多的事情。我将 DataGrid 绑定到 Silverlight 4 中的 DomainDataSource,并将 DomainDataSource 的 GroupDescriptors 绑定到 ComboBox,允许用户根据他们选择的值对 DataGrid 中的行进行分组。我想让他们能够单击按钮来折叠/展开所有组。我知道这可以使用 PagedCollectionView 来完成,但最终我会在代码隐藏中进行分组等。有没有一种方法可以在不使用 PagedCollectionView 的情况下完成此任务?

我知道 DataGrid.CollapseRowGroup(CollectionViewGroup collectionViewGroup, boolcollapseAllSubgroups) 方法,但我还没有找到迭代顶级组的方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

長街聽風 2024-11-11 19:05:15

这就是我的想法。它提供了扩展或折叠所有级别或特定级别的灵活性。 (如果需要,可以重构以删除重复的代码。)要在一次调用中展开或折叠所有级别的所有组,只需为 groupingLevel 参数传入“0”,并为 groupingLevel 参数传入“true” crashAllSublevels 参数。通过使用 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);
    }

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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文