WPF DataGrid 渲染速度很慢 [英] WPF DataGrid is very slow to render

查看:34
本文介绍了WPF DataGrid 渲染速度很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过使用自定义的 DataGrid 以及 WPF 中的常用数据网格.我曾尝试手动填充它们以及通过绑定填充它们.在这两种情况下,它们都很慢.

I have tried using both a customized DataGrid as well as the stock one in WPF. I have tried populating them manually as well as through bindings. In both cases they are slow.

我有一个场景,用户点击一个按钮,就会出现一个带有适当数据的 DataGrid.目前我处于概念验证模式并且只使用示例数据.我有一个包含 10 行表的数据集.

I have a scenerio where the user clicks on a button and a DataGrid appears with appropriate data. Currently I am in proof of concept mode and just using sample data. I have a DataSet with a table that has 10 rows in it.

如果我在单击按钮时没有将任何数据附加到 DataGrid,空的 DataGrid 几乎会立即显示出来,用户不会感觉到延迟.我一添加10行数据,6列,延迟大约2秒,对用户来说非常明显.

If I don't attach any data to the DataGrid when I click the button the empty DataGrid displays pretty much instantly, a user cannot perceive a delay. As soon as I add 10 rows of data, for 6 columns, the delay is about 2 seconds, very noticable to the user.

我什至尝试填充空数据,只是为了显示一个空网格,而且速度同样慢.

I even tried filling with empty data, just to get an empty grid to appear and it is equally as slow.

for (int i = 0; i < 10; i++)
    _dataGrid.Items.Add("");

我放置了一个计时器来计算从单击按钮到执行所有代码以绘制 DataGrid 时的滴答声,大约是 20 毫秒,因此代码执行得非常快,但在屏幕上是大滞后是.我尝试了 GridView,它在屏幕上的渲染速度非常快.

I put a timer to count the ticks from when the button is clicked to when all of the code is executed to draw the DataGrid and it is around 20 milliseconds, so the code executes very fast, but on the screen is where the big lag is. I tried a GridView and it renders much fast on the screen.

我听说过各种关于复杂场景下 DataGrid 绘制缓慢并使用 1000 行的报告,但这很简单,6 列 x 10 行填充空数据.

I have heard various reports of slow DataGrid drawing with complex scenarios and using 1000's of rows, but this is as simple as it gets, 6 columns by 10 rows filled with empty data.

对于只读显示,GridView 是与 DataGrid 同样可行的选择吗?

For readonly display is GridView an equally viable option to the 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);

这是我绑定 10 行数据集的方式.

This is a how I bind the DataSet with 10 rows in it.

                _dataGrid.ItemsSource = ds.Tables[0].DefaultView;
                _dataGrid.DataContext = ds.Tables[0];

不确定我能做些什么不同的事情.

Not sure what I can do differently.

推荐答案

你有吗:

  • 为网格启用 VirtualizingStackPanel.VirtualizationMode?如果没有 - 尝试设置.
  • 为 DataGrid 设置 VirtualizingStackPanel.IsVirtualizing="true"
  • 用 StackPanel 容器包装了一个网格?如果是 - 尝试删除.
  • 通过外部 ScrollViewer 控件包装了一个网格?如果是 - 尝试删除.
  • Enabled VirtualizingStackPanel.VirtualizationMode for a Grid? if not - try to set.
  • Set VirtualizingStackPanel.IsVirtualizing="true" for DataGrid
  • Wrapped up a Grid by a StackPanel container? If yes - try to remove.
  • Wrapped up a Grid by an external ScrollViewer control? If yes - try to remove.

还有一点,您可以一次绑定整个项目集合而不是将每个项目添加到 grid.Items 集合中吗?

One more point, could you bind whole items collection at once instead of adding each item into the grid.Items collection?

这篇关于WPF DataGrid 渲染速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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