Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set grid column or grid row in C# code behind

I have set ColumnDefinitions in XAML like:

<Grid x:Name="MainGrid"  Background="#FF256BD1">
<Grid.ColumnDefinitions>
 <ColumnDefinition Width="*"/>
 <ColumnDefinition Width="*"/>
 <ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

...
</Grid>

In the code behind I'm adding a StackPanel as children in the MainGrid like:

MainGrid.Children.Add(stackPanel);

How to put this stackPanel in the First Column in Code behind like we do in XAML as: Grid.Column="0" or Grid.ColumnSpan="2" or Grid.Row="1", etc.

like image 360
Kishor Bikram Oli Avatar asked Aug 12 '16 05:08

Kishor Bikram Oli


1 Answers

You can make use of the SetRow, SetColumn, SetColumnSpan Methods:

Grid.Children.Add(stackPanel);
Grid.SetRow(stackPanel, 1);
Grid.SetColumn(stackPanel, 0);
Grid.SetColumnSpan(stackPanel, 2);
like image 165
Zein Makki Avatar answered Nov 03 '22 06:11

Zein Makki