[英]How can I speed up adding controls to a WPF grid?
For a personal project, I need to dynamically populate a grid, based on the contents of an array of variable size. 对于个人项目,我需要根据可变大小数组的内容动态填充网格。 I use code along the lines of what is below to do that, and it works well, except that when the array grows large (as in 200 x 200 or more) it becomes slow (20+ secs to populate).
我按照下面的方法使用代码来做到这一点,并且效果很好,除了当数组变大(如200 x 200或更大)时,它变慢了(填充了20秒以上)。 It looks like instantiating the buttons is fast, but adding to the grid is slow.
看起来,实例化按钮的速度很快,但是添加到网格的速度却很慢。
Am I doing anything wrong? 我做错什么了吗? Is there anything I could do to speed up the process using the regular WPF grid?
使用常规WPF网格,我可以做些什么来加快此过程吗? Should I look at another control?
我应该看看另一个控件吗? Thanks in advance for any suggestions.
在此先感谢您的任何建议。
int columns=200;
int rows=200;
var width = new GridLength(30);
var height = new GridLength(25);
for (int column = 0; column < columns; column++)
{
var columnDefinition = new ColumnDefinition();
columnDefinition.Width = width;
this.TestGrid.ColumnDefinitions.Add(columnDefinition);
}
for (int row = 0; row < rows; row++)
{
var rowDefinition = new RowDefinition();
rowDefinition.Height = height;
this.TestGrid.RowDefinitions.Add(rowDefinition);
}
for (int column = 0; column < columns; column++)
{
for (int row = 0; row < rows; row++)
{
var button = new Button();
button.Content = row.ToString() + ", " + column.ToString();
Grid.SetRow(button, row);
Grid.SetColumn(button, column);
this.TestGrid.Children.Add(button);
}
}
Admittedly I'm still getting my chops wet with WPF, but I'm going to go out on a limb here and say that trying to add 40,000 controls is your real bottleneck; 诚然,我仍在用WPF弄碎印章,但是我要四处张扬地说,尝试添加40,000个控件是您真正的瓶颈; not so much as to how you're adding the controls.
与其说您如何添加控件,不如说是。
Even if you had all 40,000 controls hard coded in your XAML, you'd still end up with a 20+ second load time. 即使您在XAML中将所有40,000个控件都进行了硬编码,但最终仍然需要20秒钟以上的加载时间。
Either this is the world's largest data entry form or a massive Mine Sweeper board ;-) 这是世界上最大的数据输入表,还是庞大的扫雷板;-)
Have you tried surrounding your loop in: 您是否尝试过围绕以下循环:
GridView.BeginUpdate();
// add items
GridView.EndUpdate();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.