Row Definitions for Grid element
To add row definitions to Grid you need to add following lines to your XAML:
<Grid x:Name="myGrid" Height="130" Width="130" VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="10"/>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>
</Grid>
To add row definitions to Grid programmatically(using C#) use the following code:
myGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
To remove all row definitions from Grid programmatically(using C#) use the following code:
// clear row definitions
foreach (RowDefinition rowDef in myGrid.RowDefinitions.ToList())
{
myGrid.RowDefinitions.Remove(rowDef);
}
To specify a row index of a grid for your element simply add a Grid.Row attribute to your XAML, for example:
<Grid x:Name="ContentGrid" Grid.Row="1"></Grid>
Or it can also be done using C# code in codebehind:
TextBlock tb = new TextBlock();
Grid.SetRow(tb, i);
Column Definitions for Grid element¶
To add column definitions to Grid you need to add following lines to your XAML:
<Grid x:Name="myGrid" Height="130" Width="130" VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="60"/>
</Grid.ColumnDefinitions>
</Grid>
To add column definitions to Grid programmatically(using C#) use the following code:
myGrid.RowDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
To remove all column definitions from Grid programmatically(using C#) use the following code:
// clear column definitions
foreach (ColumnDefinition colDef in myGrid.ColumnDefinitions.ToList())
{
myGrid.ColumnDefinitions.Remove(colDef);
}
To specify a column index of a grid for your element simply add a Grid.Column attribute to your XAML, for example:
<Grid x:Name="ContentGrid" Grid.Column="1"></Grid>
Or it can also be done using C# code in codebehind:
TextBlock tb = new TextBlock();
Grid.SetColumn(tb, i);