WPF Layout: Grid

Proper layout and positioning are a vital part of interactive, high-performance and user-friendly Windows applications. This series of articles explains the layout process in WPF. The series starts with an understanding of the WPF layout process. The next part of this series will cover the basics of layout and positioning, such as size, margin, padding and alignment of elements. Later in this series, I will cover various panels and related parent controls available in WPF.

Table of Contents

Introduction

In the previous article, WPF Layout: DockPanel, I discussed the DockPanel. This article focuses on the Grid panel in details.

Grid Panel

The Grid Panel is the most complicated but versatile panel among all panels. A Grid panel can be used to design complicated user interfaces where we need to place multiple elements in a tabular format of rows and columns.

The Grid element in XAML represents a Grid panel. The following code snippet creates a Grid element and sets its background, width, height, vertical and horizontal alignment properties.

  1. <Grid Name="GridPanel" Background="Blue" Width="280" Height="220" VerticalAlignment="Top" HorizontalAlignment="Center" >   
  2. </Grid>   
The output looks as in Figure 1.



Figure 1

Grid Properties

A Grid has the three major properties, RowDefinitions, ColumnDefinitions and ShowGridLines. The RowDefinitions property is a collection of RowDefintions. The ColumnDefinitions property represents a collection of ColumnDefinition. The ShowGridLines property determines whether grid lines of a Grid panel are visible or not.

Create Grid

The Grid element in XAML represents a WPF Grid control. The following code snippet creates a Grid control, sets it width and foreground color and ensures the grid lines are visible.

  1. <Grid Name="MCGrid" Width="400" Background="LightSteelBlue" ShowGridLines="True" \>  
The ColumnDefinitions property is used to add columns and the RowDefinitions property is used to add rows to a Grid. The following code snippet adds three columns and three rows to a grid.
  1. <Grid.ColumnDefinitions>  
  2.     <ColumnDefinition />  
  3.     <ColumnDefinition />  
  4.     <ColumnDefinition />  
  5. </Grid.ColumnDefinitions>  
  6. <Grid.RowDefinitions>  
  7.     <RowDefinition Height="45" />  
  8.     <RowDefinition Height="45" />  
  9.     <RowDefinition Height="45" />  
  10. </Grid.RowDefinitions>   
Any control in WPF can be placed within a grid using its Grid.Row and Grid.Column properties that represents what column and what row a control will be placed in. The values of rows and columns start with 0. That means, if there are three columns in a grid, the first column would be represented by the number 0. The following code snippet puts a TextBlock control in a cell that is in second row and third column.
  1. <TextBlock Grid.Row="1" Grid.Column="2" Foreground="Green" Text="Age" Height="20" VerticalAlignment="Top" />  
Listing 1 is the complete code to create a Grid with three columns and three rows and some text data in the grid cells.
  1. <Window x:Class="GridSample.Window1"  
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  4. Title="Window1" Height="300" Width="450" WindowStyle="ThreeDBorderWindow">  
  5.     <Grid Name="MCGrid" Width="400" Background="LightSteelBlue" ShowGridLines="True">  
  6.         <Grid.ColumnDefinitions>  
  7.             <ColumnDefinition />  
  8.             <ColumnDefinition />  
  9.             <ColumnDefinition />  
  10.         </Grid.ColumnDefinitions>  
  11.         <Grid.RowDefinitions>  
  12.             <RowDefinition Height="45" />  
  13.             <RowDefinition Height="45" />  
  14.             <RowDefinition Height="45" />  
  15.         </Grid.RowDefinitions>  
  16.         <TextBlock FontSize="14" FontWeight="Bold" Grid.Row="0" Grid.Column="0" Foreground="Green"   
  17. Text="Author Name" Height="20" VerticalAlignment="Top" />  
  18.         <TextBlock FontSize="14" FontWeight="Bold" Grid.Row="0" Grid.Column="1" Foreground="Green"   
  19. Text="Age" Height="20" VerticalAlignment="Top" />  
  20.         <TextBlock FontSize="14" FontWeight="Bold" Grid.Row="0" Grid.Column="2" Foreground="Green"   
  21. Text="Book" Height="20" VerticalAlignment="Top"/>  
  22.         <TextBlock FontSize="12" Grid.Row="1" Grid.Column="0">Mahesh Chand</TextBlock>  
  23.         <TextBlock FontSize="12" Grid.Row="1" Grid.Column="1">33</TextBlock>  
  24.         <TextBlock FontSize="12" Grid.Row="1" Grid.Column="2">GDI+ Programming</TextBlock>  
  25.         <TextBlock FontSize="12" Grid.Row="2" Grid.Column="0">Mike Gold</TextBlock>  
  26.         <TextBlock FontSize="12" Grid.Row="2" Grid.Column="1">35</TextBlock>  
  27.         <TextBlock FontSize="12" Grid.Row="2" Grid.Column="2">Programming C#</TextBlock>  
  28.     </Grid>  
  29. </Window>  

Listing 1

The output looks as in this Figure 2.



Figure 2

Create a Grid Dynamically

The Grid class in WPF represents a Grid control. The following code snippet in Listing 2 creates a Grid control, sets its width, horizontal alignment, vertical alignment and shows grid lines and sets a background color.

  1. Grid DynamicGrid = new Grid();   
  2. DynamicGrid.Width = 400;   
  3. DynamicGrid.HorizontalAlignment = HorizontalAlignment.Left;   
  4. DynamicGrid.VerticalAlignment = VerticalAlignment.Top;   
  5. DynamicGrid.ShowGridLines = true;   
  6. DynamicGrid.Background = new SolidColorBrush(Colors.LightSteelBlue);  

Listing 2

The following code snippet in Listing 3 adds three columns and three rows to a Grid.

  1. // Create Columns   
  2. ColumnDefinition gridCol1 = new ColumnDefinition();   
  3. ColumnDefinition gridCol2 = new ColumnDefinition();   
  4. ColumnDefinition gridCol3 = new ColumnDefinition();   
  5. DynamicGrid.ColumnDefinitions.Add(gridCol1);   
  6. DynamicGrid.ColumnDefinitions.Add(gridCol2);   
  7. DynamicGrid.ColumnDefinitions.Add(gridCol3);   
  8.   
  9. // Create Rows   
  10. RowDefinition gridRow1 = new RowDefinition();   
  11. gridRow1.Height = new GridLength(45);   
  12. RowDefinition gridRow2 = new RowDefinition();   
  13. gridRow2.Height = new GridLength(45);   
  14. RowDefinition gridRow3 = new RowDefinition();   
  15. gridRow3.Height = new GridLength(45);   
  16.   
  17. DynamicGrid.RowDefinitions.Add(gridRow1);   
  18. DynamicGrid.RowDefinitions.Add(gridRow2);   
  19. DynamicGrid.RowDefinitions.Add(gridRow3);  

Listing 3

Once rows and columns are added to a Grid, you can add any contents to the Grid cells using SetRow and SetColumn methods. The SetRow and SetColumn methods take the first parameter as the control name and second parameter as row number and column number respectively. The following code snippet in Listing 4 creates a TextBlock control and displays it in Cell(0,0) that represents the first row and first column of Grid.

  1. // Add first column header   
  2. TextBlock txtBlock1 = new TextBlock();   
  3. txtBlock1.Text = "Author Name";   
  4. txtBlock1.FontSize = 14;   
  5. txtBlock1.FontWeight = FontWeights.Bold;   
  6. txtBlock1.Foreground = new SolidColorBrush(Colors.Green);   
  7. txtBlock1.VerticalAlignment = VerticalAlignment.Top;   
  8. Grid.SetRow(txtBlock1, 0);   
  9. Grid.SetColumn(txtBlock1, 0);  

Listing 4

Once a control is created and its position within the Grid is set, the next step is to add the control to a Grid using the Grid.Children.Add method. The code snippet in Listing 5 adds a TextBlock to a Grid.

  1. DynamicGrid.Children.Add(txtBlock1);  

Listing 5

The complete code is listed in Listing 6.

  1. private void CreateDynamicWPFGrid()   
  2. {  
  3.     // Create the Grid   
  4.     Grid DynamicGrid = new Grid();  
  5.     DynamicGrid.Width = 400;  
  6.     DynamicGrid.HorizontalAlignment = HorizontalAlignment.Left;  
  7.     DynamicGrid.VerticalAlignment = VerticalAlignment.Top;  
  8.     DynamicGrid.ShowGridLines = true;  
  9.     DynamicGrid.Background = new SolidColorBrush(Colors.LightSteelBlue);  
  10.   
  11.     // Create Columns   
  12.     ColumnDefinition gridCol1 = new ColumnDefinition();  
  13.     ColumnDefinition gridCol2 = new ColumnDefinition();  
  14.     ColumnDefinition gridCol3 = new ColumnDefinition();  
  15.     DynamicGrid.ColumnDefinitions.Add(gridCol1);  
  16.     DynamicGrid.ColumnDefinitions.Add(gridCol2);  
  17.     DynamicGrid.ColumnDefinitions.Add(gridCol3);  
  18.   
  19.     // Create Rows   
  20.     RowDefinition gridRow1 = new RowDefinition();  
  21.     gridRow1.Height = new GridLength(45);  
  22.     RowDefinition gridRow2 = new RowDefinition();  
  23.     gridRow2.Height = new GridLength(45);  
  24.     RowDefinition gridRow3 = new RowDefinition();  
  25.     gridRow3.Height = new GridLength(45);  
  26.     DynamicGrid.RowDefinitions.Add(gridRow1);  
  27.     DynamicGrid.RowDefinitions.Add(gridRow2);  
  28.     DynamicGrid.RowDefinitions.Add(gridRow3);  
  29.   
  30.     // Add first column header   
  31.     TextBlock txtBlock1 = new TextBlock();  
  32.     txtBlock1.Text = "Author Name";  
  33.     txtBlock1.FontSize = 14;  
  34.     txtBlock1.FontWeight = FontWeights.Bold;  
  35.     txtBlock1.Foreground = new SolidColorBrush(Colors.Green);  
  36.     txtBlock1.VerticalAlignment = VerticalAlignment.Top;  
  37.   
  38.     Grid.SetRow(txtBlock1, 0);  
  39.     Grid.SetColumn(txtBlock1, 0);  
  40.     // Add second column header   
  41.     TextBlock txtBlock2 = new TextBlock();  
  42.     txtBlock2.Text = "Age";  
  43.     txtBlock2.FontSize = 14;  
  44.     txtBlock2.FontWeight = FontWeights.Bold;  
  45.     txtBlock2.Foreground = new SolidColorBrush(Colors.Green);  
  46.     txtBlock2.VerticalAlignment = VerticalAlignment.Top;  
  47.     Grid.SetRow(txtBlock2, 0);  
  48.     Grid.SetColumn(txtBlock2, 1);  
  49.   
  50.     // Add third column header   
  51.     TextBlock txtBlock3 = new TextBlock();  
  52.     txtBlock3.Text = "Book";  
  53.     txtBlock3.FontSize = 14;  
  54.     txtBlock3.FontWeight = FontWeights.Bold;  
  55.     txtBlock3.Foreground = new SolidColorBrush(Colors.Green);  
  56.     txtBlock3.VerticalAlignment = VerticalAlignment.Top;  
  57.     Grid.SetRow(txtBlock3, 0);  
  58.     Grid.SetColumn(txtBlock3, 2);  
  59.   
  60.     //// Add column headers to the Grid   
  61.   
  62.     DynamicGrid.Children.Add(txtBlock1);  
  63.     DynamicGrid.Children.Add(txtBlock2);  
  64.     DynamicGrid.Children.Add(txtBlock3);  
  65.   
  66.     // Create first Row   
  67.   
  68.     TextBlock authorText = new TextBlock();  
  69.     authorText.Text = "Mahesh Chand";  
  70.     authorText.FontSize = 12;  
  71.     authorText.FontWeight = FontWeights.Bold;  
  72.     Grid.SetRow(authorText, 1);  
  73.     Grid.SetColumn(authorText, 0);  
  74.     TextBlock ageText = new TextBlock();  
  75.     ageText.Text = "33";  
  76.     ageText.FontSize = 12;  
  77.     ageText.FontWeight = FontWeights.Bold;  
  78.     Grid.SetRow(ageText, 1);  
  79.     Grid.SetColumn(ageText, 1);  
  80.     TextBlock bookText = new TextBlock();  
  81.     bookText.Text = "GDI+ Programming";  
  82.     bookText.FontSize = 12;  
  83.     bookText.FontWeight = FontWeights.Bold;  
  84.     Grid.SetRow(bookText, 1);  
  85.     Grid.SetColumn(bookText, 2);  
  86.   
  87.     // Add first row to Grid   
  88.     DynamicGrid.Children.Add(authorText);  
  89.     DynamicGrid.Children.Add(ageText);  
  90.     DynamicGrid.Children.Add(bookText);  
  91.   
  92.     // Create second row   
  93.     authorText = new TextBlock();  
  94.     authorText.Text = "Mike Gold";  
  95.     authorText.FontSize = 12;  
  96.     authorText.FontWeight = FontWeights.Bold;  
  97.     Grid.SetRow(authorText, 2);  
  98.     Grid.SetColumn(authorText, 0);  
  99.     ageText = new TextBlock();  
  100.     ageText.Text = "35";  
  101.     ageText.FontSize = 12;  
  102.     ageText.FontWeight = FontWeights.Bold;  
  103.     Grid.SetRow(ageText, 2);  
  104.     Grid.SetColumn(ageText, 1);  
  105.   
  106.     bookText = new TextBlock();  
  107.     bookText.Text = "Programming C#";  
  108.     bookText.FontSize = 12;  
  109.     bookText.FontWeight = FontWeights.Bold;  
  110.     Grid.SetRow(bookText, 2);  
  111.     Grid.SetColumn(bookText, 2);  
  112.   
  113.     // Add second row to Grid   
  114.     DynamicGrid.Children.Add(authorText);  
  115.     DynamicGrid.Children.Add(ageText);  
  116.     DynamicGrid.Children.Add(bookText);  
  117.   
  118.     // Display grid into a Window   
  119.     RootWindow.Content = DynamicGrid;  
  120. }   

Listing 6

Managing Column Width and Row Height

The ColumnDefinition has three properties that are used to manage the width of a column in a Grid. These properties are Width, MaxWidth and MinWidth. The Width property represents the width of a column. The MaxWidth and MinWidth are used to set maximum and minimum width of a column.

The RowDefinition has three properties that are used to manage the height of a row in a Grid. These properties are Height, MaxHeight and MinHeight. The Height property represents the height of a row. The MaxHeight and MinHeight are used to set maximum and minimum height of a row.

The code listed in Listing 7 sets the width of columns and height of rows in a Grid panel at design-time using XAML.

  1. <Grid Name="MCGrid" VerticalAlignment="Top"   
  2. HorizontalAlignment="Left" Background="LightSteelBlue" ShowGridLines="False">  
  3.     <Grid.ColumnDefinitions>  
  4.         <ColumnDefinition Width="120"/>  
  5.         <ColumnDefinition Width="50" />  
  6.         <ColumnDefinition Width="250"/>  
  7.     </Grid.ColumnDefinitions>  
  8.     <Grid.RowDefinitions>  
  9.         <RowDefinition Height="25" />  
  10.         <RowDefinition Height="22" />  
  11.         <RowDefinition Height="22" />  
  12.         <RowDefinition Height="22" />  
  13.         <RowDefinition Height="22" />  
  14.         <RowDefinition Height="22" />  
  15.     </Grid.RowDefinitions>  
  16.     <TextBlock FontSize="14" FontWeight="Bold" Grid.Row="0" Grid.Column="0"   
  17. Text=" Author Name" Height="25"   
  18. VerticalAlignment="Top"   
  19. Background="DarkBlue" Foreground="WhiteSmoke" />  
  20.     <TextBlock FontSize="14" FontWeight="Bold" Grid.Row="0" Grid.Column="1"   
  21. Text=" Age" Height="25" VerticalAlignment="Top"   
  22. Background="DarkBlue" Foreground="WhiteSmoke" />  
  23.     <TextBlock FontSize="14" FontWeight="Bold" Grid.Row="0" Grid.Column="2"   
  24. Text=" Book" Height="25" VerticalAlignment="Top"   
  25. Background="DarkBlue" Foreground="WhiteSmoke" />  
  26.     <TextBlock Grid.Row="1" Grid.Column="0">Mahesh Chand</TextBlock>  
  27.     <TextBlock Grid.Row="1" Grid.Column="1">33</TextBlock>  
  28.     <TextBlock Grid.Row="1" Grid.Column="2">GDI+ Programming</TextBlock>  
  29.     <TextBlock Grid.Row="2" Grid.Column="0">John Trotter</TextBlock>  
  30.     <TextBlock Grid.Row="2" Grid.Column="1">42</TextBlock>  
  31.     <TextBlock Grid.Row="2" Grid.Column="2">ADO.NET Programming Cookbook</TextBlock>  
  32.     <TextBlock Grid.Row="3" Grid.Column="0">Mike Gold</TextBlock>  
  33.     <TextBlock Grid.Row="3" Grid.Column="1">35</TextBlock>  
  34.     <TextBlock Grid.Row="3" Grid.Column="2">Programming C#</TextBlock>  
  35.     <TextBlock Grid.Row="4" Grid.Column="0">Raj Kumar</TextBlock>  
  36.     <TextBlock Grid.Row="4" Grid.Column="1">22</TextBlock>  
  37.     <TextBlock Grid.Row="4" Grid.Column="2">XAML Development with C#</TextBlock>  
  38.     <TextBlock Grid.Row="5" Grid.Column="0">Praveen Kumar</TextBlock>  
  39.     <TextBlock Grid.Row="5" Grid.Column="1">28</TextBlock>  
  40.     <TextBlock Grid.Row="5" Grid.Column="2">WPF and Visual Studio 2010</TextBlock>  
  41. </Grid>   

Listing 7

The output of Listing 7 looks as in Figure 3.



Figure 3

Add and Remove Columns

The Add method of Grid.ColumnDefinitions adds a new column to a Grid.

  1. DynamicGrid.ColumnDefinitions.Add(new ColumnDefinition());  
The Grid.ColumnDefinitions.Insert method adds a column at a given position. The following code adds a new column at position 3 in a Grid.
  1. DynamicGrid.ColumnDefinitions.Insert(3, new ColumnDefinition());  
The RemoveAt method of Grid.ColumnDefinitions deletes a column at the given position.
  1. DynamicGrid.ColumnDefinitions.RemoveAt(3);  
The Clear method of Grid.ColumnDefinitions deletes all columns in a Grid.
  1. DynamicGrid.ColumnDefinitions.Clear();  
Add and Remove Rows

The Add method of Grid.RowDefinitions adds a new row to a Grid.
  1. DynamicGrid.RowDefinitions.Add(new RowDefinition());  
The Grid.RowDefinitions.Insert method adds a row at a given position.
  1. DynamicGrid.RowDefinitions.Insert(3, new RowDefinition ());  
The RemoveAt method of Grid.RowDefinitions deletes a row at the given position.
  1. DynamicGrid.RowDefinitions.RemoveAt(3);  
The Clear method of Grid.RowDefinitions deletes all rows in a Grid.
  1. DynamicGrid.RowDefinitions.Clear();  
Resize WPF Grid Rows with a GridSplitter

The following code snippet in Listing 8 adds a Grid splitter to a Grid that you can use to resize a Grid row.
  1. <Grid Name="DynamicGrid" Width="466" Background="LightSteelBlue" ShowGridLines="True"   
  2. Canvas.Top="119" Canvas.Left="8" Height="200">  
  3.     <Grid.ColumnDefinitions>  
  4.         <ColumnDefinition />  
  5.         <ColumnDefinition />  
  6.         <ColumnDefinition />  
  7.     </Grid.ColumnDefinitions>  
  8.     <Grid.RowDefinitions>  
  9.         <RowDefinition Height="50*" />  
  10.         <RowDefinition Height="Auto" />  
  11.         <RowDefinition Height="50*" />  
  12.     </Grid.RowDefinitions>  
  13.     <GridSplitter   
  14. ResizeDirection="Rows"   
  15. Grid.Column="0"   
  16. Grid.ColumnSpan="10"   
  17. Grid.Row="1"   
  18. Width="Auto"   
  19. Height="3"   
  20. HorizontalAlignment="Stretch"   
  21. VerticalAlignment="Stretch"   
  22. Margin="0"   
  23. Background="Green"/>  
  24. </Grid>   

Listing 8

Formatting Grid

The Background property of a Grid sets the background colors of a Grid. The following code snippet uses linear gradient brushes to draw the background of a Grid.

  1. <Grid.Background>  
  2.     <LinearGradientBrush StartPoint="0,0" EndPoint="1,1" >  
  3.         <GradientStop Color="Blue" Offset="0.1" />  
  4.         <GradientStop Color="Orange" Offset="0.25" />  
  5.         <GradientStop Color="Green" Offset="0.75" />  
  6.         <GradientStop Color="Red" Offset="1.0" />  
  7.     </LinearGradientBrush>  
  8. </Grid.Background>   
The new Grid looks as in Figure 4.



Figure 4

Setting Image as Background of a Grid

To set an image as the background of a Grid, we can set an image as the Background of the Grid. The following code snippet sets the background of a Grid to an image. The code also sets the opacity of the image.

  1. <Grid.Background>  
  2.     <ImageBrush ImageSource="Flower.jpg" Opacity="0.3"/>  
  3. </Grid.Background>   
The new output looks as in Figure 5.



Figure 5

Summary

This article explained the Grid panel in WPF. In the next article of this series, I will focus on the Stack panel in details.


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.