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

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

问题描述

我使用的是 WPF的DataGrid从codePLEX ,我需要让虚拟化与分组工作。

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.

分组和虚拟化似乎是一个pretty的常见用例的网格。有没有一个标准/推荐/简单的得到这种的方式?

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中)。不幸的是,仍然没有虚拟化的一次分组上使用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.

假设你使用的是项目presenter你GroupItem的模板的扩展中,默认情况下你的膨胀不扩大,然后尝试简单的绑定你的项目presenter到扩展的IsEnabled属性与知名度默认BooleanToVisibilityConverter:

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需要很长的加载(因为它本质上是画在你的数据网格,即使它是在一个倒塌的膨胀每一条记录)这个问题......那么使用上述code会导致数据网格不绘制记录,直到你展开一个组,然后,它只会抽出记录特定群体。

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一个新的附加属性<一个href="http://msdn.microsoft.com/en-us/library/system.windows.controls.virtualizingpanel.isvirtualizingwhengrouping%28v=vs.110%29.aspx">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天全站免登陆