Grid Layout in WPF

WPF Grid panel enables you to arrange children elements in cells defined by rows and columns. In the code example of this article, we will learn Grid layout and its properties in WPF using C# and XAML.

A Grid is a very powerful and useful Layout in WPF. It enables you to arrange children elements in cells defined by rows and columns. In fact, when we add a new XAML document or create a new WPF Project in Visual Studio, Visual Studio automatically adds a Grid as the first container inside the window element.
 
We can create Rows and Columns in the following two ways.

First Method: By typing XAML Code

By default, a Grid has one row and one column. We can add more rows and columns by adding a RowDefinition Element for each row inside the Grid.RowDefinitions property and a ColumnDefinition Element for each column inside the Grid.ColumnDefinitions property. By default, GridLines are invisible. For showing the GridLines, set the ShowGridLines property of the Grid to True. GridLines are helpful during debugging for determining which element is in which cell.
 
Let's understand it with a simple example. In this example, we have created 3 rows and 3 columns. Then added 9 TextBlocks and mainted the TextBlock position inside the grid by specifying the Grid.Row and Grid.Column values. If we don't specify a Grid.Row and Grid.Column property then an Element is placed in Grid.Row="0" and Grid.Column="0", in other words in First Row and First Column.
  1. <Window x:Class="GridLayoutExample1.MainWindow"    
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
  4.         Title="MainWindow" Height="350" Width="525">    
  5.     <Grid ShowGridLines="True">    
  6.         <Grid.ColumnDefinitions>    
  7.             <ColumnDefinition></ColumnDefinition>    
  8.             <ColumnDefinition></ColumnDefinition>    
  9.             <ColumnDefinition></ColumnDefinition>    
  10.         </Grid.ColumnDefinitions>    
  11.         <Grid.RowDefinitions>    
  12.             <RowDefinition></RowDefinition>    
  13.             <RowDefinition></RowDefinition>    
  14.             <RowDefinition></RowDefinition>    
  15.         </Grid.RowDefinitions>    
  16.         <TextBlock Text="Row0 Column0" Grid.Row="0" Grid.Column="0" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>    
  17.         <TextBlock Text="Row0 Column1" Grid.Row="0" Grid.Column="1" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>    
  18.         <TextBlock Text="Row0 Column2" Grid.Row="0" Grid.Column="2" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>    
  19.         <TextBlock Text="Row1 Column0" Grid.Row="1" Grid.Column="0" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>    
  20.         <TextBlock Text="Row1 Column1" Grid.Row="1" Grid.Column="1" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>    
  21.         <TextBlock Text="Row1 Column2" Grid.Row="1" Grid.Column="2" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>    
  22.         <TextBlock Text="Row2 Column0" Grid.Row="2" Grid.Column="0" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>    
  23.         <TextBlock Text="Row2 Column1" Grid.Row="2" Grid.Column="1" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>    
  24.         <TextBlock Text="Row2 Column2" Grid.Row="2" Grid.Column="2" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>    
  25.     </Grid>    
  26. </Window>    
Preview

 

Second Method: By Clicking on Margin

 
 
We can control a column's width and a row's height in the following 3 ways.
 
1) Automatic Sizing

Rows and columns are sized automatically to fit the content/child element.
 
Example:
  1. <Grid ShowGridLines="True">    
  2.         <Grid.ColumnDefinitions>    
  3.             <ColumnDefinition Width="auto"></ColumnDefinition>    
  4.             <ColumnDefinition Width="auto"></ColumnDefinition>    
  5.         </Grid.ColumnDefinitions>    
  6.         <Grid.RowDefinitions>    
  7.             <RowDefinition Height="auto"></RowDefinition>    
  8.             <RowDefinition Height="auto"></RowDefinition>    
  9.         </Grid.RowDefinitions>    
  10.         <Button Content="Row0 column0" FontSize="16" Grid.Row="0" Grid.Column="0"></Button>    
  11.         <Button Content="Row0 column1" FontSize="16" Grid.Row="0" Grid.Column="1"></Button>    
  12. </Grid>    
Preview

 
 
2) Absolute Sizing

In Absolute Sizing, the exact size of the height and width is specified in RowDefinition and ColumnDefinition. The size of the row and column does not shrink or expand when the size of the Grid/window is changed.
  1. <Grid ShowGridLines="True">    
  2.         <Grid.ColumnDefinitions>    
  3.             <ColumnDefinition Width="200"></ColumnDefinition>    
  4.             <ColumnDefinition Width="325"></ColumnDefinition>    
  5.         </Grid.ColumnDefinitions>    
  6.         <Grid.RowDefinitions>    
  7.             <RowDefinition Height="100"></RowDefinition>    
  8.             <RowDefinition Height="100"></RowDefinition>    
  9.             <RowDefinition Height="150"></RowDefinition>    
  10.         </Grid.RowDefinitions>    
  11.         <TextBlock Text="Row0 Column0" Grid.Row="0" Grid.Column="0" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>    
  12.         <TextBlock Text="Row0 Column1" Grid.Row="0" Grid.Column="1" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>    
  13.         <TextBlock Text="Row1 Column0" Grid.Row="1" Grid.Column="0" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>    
  14.         <TextBlock Text="Row1 Column1" Grid.Row="1" Grid.Column="1" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>    
  15.         <TextBlock Text="Row2 Column0" Grid.Row="2" Grid.Column="0" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>    
  16.         <TextBlock Text="Row2 Column1" Grid.Row="2" Grid.Column="1" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>    
  17. </Grid>    
Preview

 
 
3) Proportional Sizing

In proportional sizing, the available space is divided proportionally among columns and rows.
 
For example, I have created 3 columns of width *,*,2*. That means the width of Column0 is 1/4th, the width of column1 is 1/4th and the width of column2 is 2/4th of the grid. In the same way, I have divided the height of the rows. A proportional-sized row or column shrinks and grows in size on the changing of the grid size.
  1. <Grid ShowGridLines="True">    
  2.         <Grid.ColumnDefinitions>    
  3.             <ColumnDefinition Width="*"></ColumnDefinition>    
  4.             <ColumnDefinition Width="*"></ColumnDefinition>    
  5.             <ColumnDefinition Width="2*"></ColumnDefinition>    
  6.         </Grid.ColumnDefinitions>    
  7.         <Grid.RowDefinitions>    
  8.             <RowDefinition Height="*"></RowDefinition>    
  9.             <RowDefinition Height="3*"></RowDefinition>    
  10.         </Grid.RowDefinitions>    
  11.         <TextBlock Text="Row0 Column0" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>    
  12.         <TextBlock Text="Row0 Column1" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>    
  13.         <TextBlock Text="Row0 Column2" Grid.Row="0" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>    
  14.         <TextBlock Text="Row1 Column0" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>    
  15.         <TextBlock Text="Row1 Column1" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>    
  16.         <TextBlock Text="Row1 Column2" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>    
  17. </Grid>
 
Preview

 
 
Using a Grid is much similar to working with a table in HTML. We can also merge/span rows and columns using the Grid.ColumnSpan and Grid.RowSpan property.
  1. <Grid ShowGridLines="True">    
  2.         <Grid.ColumnDefinitions>    
  3.             <ColumnDefinition Width="*"></ColumnDefinition>    
  4.             <ColumnDefinition Width="*"></ColumnDefinition>    
  5.             <ColumnDefinition Width="*"></ColumnDefinition>    
  6.         </Grid.ColumnDefinitions>    
  7.         <Grid.RowDefinitions>    
  8.             <RowDefinition Height="*"></RowDefinition>    
  9.             <RowDefinition Height="*"></RowDefinition>    
  10.             <RowDefinition Height="2*"></RowDefinition>    
  11.         </Grid.RowDefinitions>    
  12.         <TextBlock Text="Merging top 3 Cells" Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18"></TextBlock>    
  13.         <TextBlock Text="Rowspan=2" Grid.Row="1" Grid.Column="0" Grid.RowSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18"></TextBlock>    
  14.         <TextBlock Text="Rowspan=2 Colspan=2" Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18"></TextBlock>    
  15. </Grid>   
Preview

 
 
I hope you like it. Thanks.