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

查看:910
本文介绍了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的行,但这是

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天全站免登陆