Skip to content

Instantly share code, notes, and snippets.

@arnabdas
Created September 19, 2013 19:04
Show Gist options
  • Select an option

  • Save arnabdas/6628321 to your computer and use it in GitHub Desktop.

Select an option

Save arnabdas/6628321 to your computer and use it in GitHub Desktop.
Dynamically add rows and columns in WPF
ContentPanel.ColumnDefinitions.Clear();
ContentPanel.RowDefinitions.Clear();
int numColumnsToAdd = 4;
int numRowsToAdd = 4;
for (int column = 0; column < numColumnsToAdd; column++)
{
ColumnDefinition cd = new ColumnDefinition();
cd.Width = new GridLength(200);
ContentPanel.ColumnDefinitions.Add(cd);
}
for (int row = 0; row < numRowsToAdd; row++)
{
RowDefinition rd = new RowDefinition();
rd.Height = new GridLength(200);
ContentPanel.RowDefinitions.Add(rd);
}
//add an image into each
for (int row = 0; row < numRowsToAdd; row++)
{
for(int col = 0; col < numColumnsToAdd; col++)
{
TextBlock txt = new TextBlock();
txt.Text = "Hello";
ContentPanel.Children.Add(txt);
Grid.SetColumn(txt, col);
Grid.SetRow(txt, row);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment