简体   繁体   English

ScrollViewer 使用 DataGrid 降低性能

[英]ScrollViewer slow performance with DataGrid

I have the following scenario:我有以下场景:

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

Now, when I bind DataGrid to large amount of data (around 10.000 rows) I am having very slow perfomance.现在,当我将 DataGrid 绑定到大量数据(大约 10.000 行)时,我的性能非常慢。 In fact, i get OutOfmemory exception (and I have 8 GB memory)!事实上,我得到 OutOfmemory 异常(我有 8 GB 内存)! I read somewhere that this is because ScrollViewer overrides DataGrid virtualisation (or something like that), but I don't know how to prevent that.我在某处读到这是因为 ScrollViewer 覆盖了 DataGrid 虚拟化(或类似的东西),但我不知道如何防止这种情况发生。 If I remove the ScrollViewer, problem solved!如果我删除 ScrollViewer,问题就解决了! The data loads in less than a second.数据加载不到一秒钟。

I want to keep the ScrollViewer (because of other controls) and have good performance.我想保留 ScrollViewer(因为其他控件)并具有良好的性能。 Is that possible?那可能吗? If not, is there any other solution-workaround?如果没有,是否还有其他解决方法?

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.此类问题的常见解决方法是在与DataGrid相同的行中添加一个不可见的“调整大小元素”,然后您可以将DataGrid.Height绑定到调整大小元素的ActualHeight This way, your DataGrid will always consume the Height of the RowDefinition .这样,您的DataGrid将始终使用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>

The outer ScrollViewer effectively gives the DataGrid as much space as it likes, that way its height becomes huge, showing all rows at once.外部 ScrollViewer 有效地为 DataGrid 提供了尽可能多的空间,这样它的高度就会变得很大,一次显示所有行。 Just restrict the DataGrid by explicitly setting a height on it for example.例如,只需通过在其上显式设置高度来限制 DataGrid。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM