ScrollViewer使用DataGrid会降低性能 [英] ScrollViewer slow performance with DataGrid

查看:83
本文介绍了ScrollViewer使用DataGrid会降低性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

<ScrollViewer>
    <Grid>
         <!--many other controls-->
         <DataGrid />
    </Grid>
</ScrollViewer>

现在,当我将DataGrid绑定到大量数据(大约10.000行)时,我的速度非常慢性能。实际上,我收到OutOfmemory异常(并且我有8 GB内存)!我在某处读到这是因为ScrollViewer覆盖了DataGrid虚拟化(或类似的东西),但是我不知道如何防止这种情况。如果删除ScrollViewer,问题就解决了!数据加载时间不到一秒钟。

Now, when I bind DataGrid to large amount of data (around 10.000 rows) I am having very slow perfomance. In fact, i get OutOfmemory exception (and I have 8 GB memory)! I read somewhere that this is because ScrollViewer overrides DataGrid virtualisation (or something like that), but I don't know how to prevent that. If I remove the ScrollViewer, problem solved! The data loads in less than a second.

我想保留ScrollViewer(由于其他控件)并具有良好的性能。那可能吗?如果没有,还有其他解决方法吗?

I want to keep the ScrollViewer (because of other controls) and have good performance. Is that possible? If not, is there any other solution-workaround?

推荐答案

这些问题的常见解决方法是添加一个不可见的尺寸元素与 DataGrid 放在同一行中,则可以将 DataGrid.Height 绑定到<$ c $尺寸元素的c> ActualHeight 。这样,您的 DataGrid 将始终消耗 RowDefinition 的高度。示例

A common workaround to these sorts of problems is to add an invisible "sizing element" in the same Row as the DataGrid, then you can bind DataGrid.Height to the ActualHeight of the sizing element. This way, your DataGrid will always consume the Height of the RowDefinition. Example

<ScrollViewer>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Button Content="Some Control.." />
        <Rectangle Name="sizingElement"
                   Grid.Row="1"
                   Fill="Transparent"
                   Margin="1"/>
        <DataGrid Grid.Row="1"
                  Height="{Binding ElementName=sizingElement,
                                   Path=ActualHeight, FallbackValue=1}">
            <!--...-->
        </DataGrid>
        <Button Content="Some more controls etc.." Grid.Row="2"/>
    </Grid>
</ScrollViewer>

这篇关于ScrollViewer使用DataGrid会降低性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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