Let's say I need to make a with 30 columns and 30 rows. Do I have to do <ColumnDefinition></ColumnDefinition>
and <RowDefinition></RowDefinition>
for 30 times? Is there any easier/faster method?
Asked
Active
Viewed 575 times
0

user2951219
- 79
- 8
-
2You can always iterate through the list of column names and add the column using code behind – Tejus Sep 18 '18 at 06:36
-
You can use `
` copy 5x. Then select all 5 and hit `Ctrl+V` 6x. - shouldn't be to much of an effort. – Felix D. Sep 18 '18 at 06:51 -
Or you could try to cluster your view by separating parts into several usercontrols. that should save you some columns and rows ! – Felix D. Sep 18 '18 at 06:53
-
What are you actually trying to do? Maybe you're better off with another control and let it generate your cells with a template. – Lennart Sep 18 '18 at 08:43
-
I am with @Lennart on this; it sounds like you probably want to use another control entierly, like a `UniformGrid` or `StackPanel`. – Élie Sep 18 '18 at 10:14
-
Possible duplicate of [Add Rows/Columns to a Grid dynamically](https://stackoverflow.com/questions/18296889/add-rows-columns-to-a-grid-dynamically) – ASh Sep 18 '18 at 11:22
1 Answers
0
Do I have to do
<ColumnDefinition></ColumnDefinition>
and<RowDefinition></RowDefinition>
for 30 times? Is there any easier/faster method?
Create them programmatically using a plain old for loop:
public MainWindow()
{
InitializeComponent();
const int n = 30;
for (int i = 0; i < n; ++i)
{
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.RowDefinitions.Add(new RowDefinition());
}
}
XAML:
<Grid x:Name="grid" />
It can't be easier.

mm8
- 163,881
- 10
- 57
- 88