Dav*_*her 35 wpf performance datagrid render
我尝试过使用自定义的DataGrid以及WPF中的库存.我尝试过手动填充它们以及通过绑定.在这两种情况下,它们都很慢.
我有一个场景,用户点击一个按钮,DataGrid会显示适当的数据.目前我在概念模式的证明,只使用样本数据.我有一个DataSet,其中包含10行.
如果我单击按钮时没有将任何数据附加到DataGrid,则空DataGrid会立即显示,用户无法察觉延迟.一旦我添加10行数据,对于6列,延迟大约为2秒,对用户来说非常明显.
我甚至尝试填充空数据,只是为了得到一个空网格,它同样慢.
for (int i = 0; i < 10; i++)
_dataGrid.Items.Add("");
Run Code Online (Sandbox Code Playgroud)
我设置了一个计时器来计算从单击按钮到执行所有代码以绘制DataGrid时的刻度,它大约是20毫秒,因此代码执行速度非常快,但在屏幕上是大滞后的地方. .我尝试了一个GridView,它在屏幕上渲染得非常快.
我听过各种关于使用复杂场景和使用1000行的DataGrid绘制缓慢的报告,但这很简单,6列10行填充空数据.
对于只读显示,GridView是DataGrid同样可行的选项吗?
更新
这是我的专栏的创建.
DataGridTextColumn column = new DataGridTextColumn();
column.ColumnWidthChanged += new ColumnWidthChangedEventHandler(column_ColumnWidthChanged);
column.Header = entity.GetPropertyValue("ColumnLabel");
column.Binding = new Binding(entity.GetPropertyValue("Tag"));
column.Width = new DataGridLength(entity.GetPropertyDouble("DisplaySize"));
_dataGrid.Columns.Add(column);
Run Code Online (Sandbox Code Playgroud)
这是我如何将DataSet绑定到其中的10行.
_dataGrid.ItemsSource = ds.Tables[0].DefaultView;
_dataGrid.DataContext = ds.Tables[0];
Run Code Online (Sandbox Code Playgroud)
不知道我能做些什么不同.
sll*_*sll 33
你有:
VirtualizingStackPanel.VirtualizationMode
是否为网格启用?如果不是 - 尝试设置.还有一点,您是否可以一次绑定整个项目集合,而不是将每个项目添加到grid.Items集合中?
Tri*_*gen 25
DataGrid
性能问题的一般提示:我在使用DataGrid时出现问题,在窗口调整大小,列排序等之后刷新了几秒钟,并锁定了窗口UI(1000行,5列) .
它归结为一个问题(bug?)与WPF大小计算.我把它放在一个网格中,RowDefinition
Height="Auto"
这导致渲染系统尝试通过测量每个列和行的大小来重新计算DataGrid的大小,可能是通过填充整个网格(据我了解).它应该以某种方式智能地处理它,但在这种情况下它不是.
快速检查以确定这是否是一个相关问题是在测试期间将DataGrid 的Height
和Width
属性设置为固定大小,然后再次尝试运行.如果您的性能已恢复,则可以在以下选项中进行永久性修复:
MaxHeight
和MaxWidth
DataGrid的固定值大于它可以得到正常使用Grid
,
DockPanel
,等)Dam*_*les 12
我在Google上找到的博客给了我一种解决方案.正如作者所说,我禁用了GroupStyle,解决了渲染速度问题.但我需要分组.作者说
VirtualizingPanel.IsVirtualizingWhenGrouping
Run Code Online (Sandbox Code Playgroud)
添加到.NET 4.5.所以我把它设置为真.现在,通过分组可以快速渲染.问题是......滚动是生涩的.没有不可接受的生涩,但明显生涩.当我尝试创建扩展了2000多个节点的TreeView时,我遇到了类似的问题.没有虚拟化,渲染速度很慢但滚动很顺利.通过虚拟化,渲染很快但滚动不稳定.
为什么我们不能同时拥有......
Eva*_*ans 11
在我的情况下,我遇到了DataGridCell ControlTemplate的问题,这减慢了渲染速度.
请注意,在ReadOnly模式下使用TextBlock(不是可选文本)或TextBox时,大数据集的相对加载速度非常不同:
加载时间59秒:
<Style TargetType="{x:Type DataGridCell}" x:Key="DataGridCellTextStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<TextBox IsReadOnly="True" Text="{Binding Mode=OneWay}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
加载时间21秒:
<Style TargetType="{x:Type DataGridCell}" x:Key="DataGridCellTextStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<ContentPresenter Content="{Binding}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
加载时间16秒:
<Style TargetType="{x:Type DataGridCell}" x:Key="DataGridCellTextStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<TextBlock Text="{Binding}"></TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
对我来说是:
<Setter Property='ScrollViewer.CanContentScroll' Value='False' />
Run Code Online (Sandbox Code Playgroud)
我从样式中删除了它,渲染变得很快。
小智 5
好一点,添加更多(我知道它的主题很老,但仍然可以帮助某人)...
我试过了
EnableColumnVirtualization="True" VirtualizingPanel.VirtualizationMode="Recycling"
EnableRowVirtualization="True"
Run Code Online (Sandbox Code Playgroud)
对于AutoGenerateColumns="True"
绑定到DataTable.DefaultView()的DataGrid()以及对速度没有影响,它对于Speed以及行之间的导航仍然是可怕的。然后,我想出了设置DataGrid的Fixed Height和Width的解决方案。另外我也设定
RowHeight="23"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.VerticalScrollBarVisibility="Visible"
Run Code Online (Sandbox Code Playgroud)
这使我的页面填充非常快...而不是2分钟,现在几乎不需要10-12秒。
希望它可以帮助某人。
注意:我正在使用.Net 4.5
我遇到了 1000 行、5 列的大问题,其中渲染时间需要 7-10 秒,但在https://www.elegant-software.net/2014/05/performance-of-the-wpf 上找到了解决方案-datagrid.html立即加载网格!
<DataGrid
EnableRowVirtualization="True"
EnableColumnVirtualization="True">
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
34211 次 |
最近记录: |