WPF DataGrid虚拟化与分组 [英] WPF DataGrid Virtualization with Grouping

查看:252
本文介绍了WPF DataGrid虚拟化与分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用来自CodePlex的WPF DataGrid ,我需要让虚拟化工作在分组。

I'm using the WPF DataGrid from CodePlex and I need to get Virtualization to work with grouping.

问题是主题并指向 MSDN示例,但它仅涵盖ListControls与简单(即单个列)DataTemplates。

This question is on topic and points to an MSDN Example but it only covers ListControls with with simple (i.e. single 'column') DataTemplates.

分组和虚拟化似乎是Grid的一个很常见的用例。有没有一个标准/推荐/简单的方式来实现这一目标?

Grouping and Virtualization seems like a pretty common use case for a Grid. Is there a standard/recommended/simple way of getting this going?

推荐答案

我意识到我迟到了这里的聚会。 ..但我最近遇到这个问题(使用内置于.NET 4的DataGrid)。不幸的是,一旦Grouping被用于DataGrid,仍然没有虚拟化的行,但是我发现一个非常光滑的性能增强技巧,希望其他人也会发现有用。

I realize I'm late to the party here... but I ran into this problem recently (using the DataGrid built into .NET 4). Unfortunately, there still is no virtualization of the rows once Grouping is used on the DataGrid... but a I found a very slick performance enhancement trick that hopefully somebody else will find useful as well.

假设您在GroupItem模板的扩展器中使用ItemsPresenter,并且默认情况下,您的扩展器不会展开,然后尝试使用默认BooleanToVisibilityConverter将ItemsPresenter的可见性绑定到Expander的IsEnabled属性:

Assuming you're using an ItemsPresenter within an expander of your GroupItem's template and by default your expander is not expanded, then try simply binding the visibility of your ItemsPresenter to the Expander's IsEnabled property with the default BooleanToVisibilityConverter:

<BooleanToVisibilityConverter x:Key="bool2vis" />


<DataGrid.GroupStyle>
    <GroupStyle>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander x:Name="exp">
                                <ItemsPresenter Visibility="{Binding ElementName=exp, Path=IsExpanded, Converter={StaticResource bool2vis}}" />
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</DataGrid.GroupStyle>

如果您遇到了DataGrid需要很长时间加载的问题(因为它基本上是绘图在数据网格中的每个记录,即使它在一个折叠的扩展器中)...然后使用上述代码将导致数据网格不能绘制您的记录,直到您展开一个组,然后,它将只绘出该特定的记录

If you're running into the problem where your DataGrid takes really long to load (because it's essentially drawing out every record in your datagrid even though it's in a collapsed expander)... then using the above code will cause the datagrid to not draw your records until you expand a group, and then, it will only draw out the records for that particular group.

缺点是这只有当您的扩展器默认折叠时仍然有帮助,并且仍然没有虚拟化行(如果扩展中有100个项目)组,但只有20个适合屏幕,所有100将在您扩展组的时候绘制)。

The down side is that this only helps if your expanders are collapsed by default, and still the rows do not get virtualized (if you have 100 items in an expanded group, but only 20 fit on the screen, all 100 will be drawn at the time you expanded the group).

上面的一点是,你基本上实现了延迟加载的DataGrid记录,因此您不需要执行绘图工作,直到您确实需要查看项目(您选择展开该组)。对于我的产品,我的组标题有内置的按钮,用于对组内的所有项目执行操作,因此用户更频繁地不会扩展组,除非他们需要对组内的单个项目执行操作。

The upside is that you've essentially implemented lazy loading of your DataGrid records, so you're not performing the drawing work until you actually need to view the items (you choose to expand the group). For my product, my group header had buttons built in to perform operations on all items within its group, so more often the user never expanded a group unless they needed to perform an operation on an individual item within a group.

*有一件事要注意,如果你使用这个技巧,你应该为列标题设置一些明确的宽度或最小宽度(因为DataGrid首次加载时没有绘制项目,所以列标题不能自动调整以适应最大的项目)

*One thing to note if you use this trick is that you should probably set some explicit widths or minimum widths to your column headers (because the items are not being drawn when the DataGrid first loads, so the column headers cannot autosize to fit the largest item).

希望真正的虚拟化在未来的服务包中实现,但如果没有,我希望这将有助于别人!

Hopefully true virtualization gets implemented in a future service pack, but if not, I hope this will help somebody else!

似乎此问题将在.NET 4.5中使用新的附加属性 VirtualizingPanel.IsVirtualizingWhenGrouping

It appears this issue will be fixed in .NET 4.5 with a new attached property VirtualizingPanel.IsVirtualizingWhenGrouping.

这篇关于WPF DataGrid虚拟化与分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆