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). 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? 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);
}
}