缓慢的ScrollViewer性能比较与数据网格 [英] ScrollViewer slow perfomance with DataGrid
问题描述
我有以下情形:
<ScrollViewer>
<Grid>
<!--many other controls-->
<DataGrid />
</Grid>
</ScrollViewer>
现在,当我绑定的DataGrid到大量的数据(约10.000行),我有很慢的性能比较的。事实上,我得到了OutOfMemory异常(我有8 GB的内存)!我读的地方,这是因为覆盖ScrollViewer的DataGrid的虚拟化(或类似的东西),但我不知道该怎么prevent的。如果我删除的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
到的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性能比较与数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!