
Margin and Alignment Properties
The precise position of a FrameworkElement object within a panel can be specified by setting the Margin, HorizontalAlignment, and VerticalAlignment properties. In the following example, a part of a form is defined by using a Grid object.
<Grid x:Name="LayoutRoot" Background="Coral" Width="300" Height="100" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Text="First Name" />
<TextBlock Grid.Column="0" Grid.Row="1" Text="Last Name" />
<TextBox Grid.Column="1" Grid.Row="0" />
<TextBox Grid.Column="1" Grid.Row="1" />
</Grid>
The following illustration shows how this code renders.
.png)
This layout looks a bit awkward because the elements are not aligned and there is no space between the objects and their container. Also, note that the two text boxes are stretched to fill their containers. This is because Stretch is the default value for the HorizontalAlignment and VerticalAlignment properties.
To position an object within its container area, use the HorizontalAlignment and VerticalAlignment properties. In the following example, the value Center is assigned to HorizontalAlignment and VerticalAlignment to position the TextBlock elements in the center of the Grid cells.
<Grid x:Name="LayoutRoot" Background="Coral" Width="300" Height="100" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Text="First Name" HorizontalAlignment="Center" VerticalAlignment="Center" />
<TextBlock Grid.Column="0" Grid.Row="1" Text="Last Name" HorizontalAlignment="Center" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="0" />
<TextBox Grid.Column="1" Grid.Row="1" />
</Grid>
The following diagram shows the effect of the HorizontalAlignment and VerticalAlignment properties.
.png)
You can use the Margin property to create a margin of space between objects. The Margin property is of type Thickness, which means that you can specify different values for the left, top, right, and bottom margins. For example, in XAML, an object that has a margin specified as Margin="20, 10, 7, 8" would have a margin of 20 pixels on the left, 10 on the top, 7 on the right, and 8 on the bottom.
<Grid x:Name="LayoutRoot" Background="Coral" Width="300" Height="100" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Text="First Name" HorizontalAlignment="Center" VerticalAlignment="Center" />
<TextBlock Grid.Column="0" Grid.Row="1" Text="Last Name" HorizontalAlignment="Center" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="0" Margin="15,10,15,10" />
<TextBox Grid.Column="1" Grid.Row="1" Margin="15,10,15,10" />
</Grid>
The following illustration shows the effect of setting the Margin property.