WPF Layout: Horizontal and Vertical Alignment

Proper layout and positioning are a vital part of interactive, high-performance and user-friendly Windows applications. This series of articles explain 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: Margins, I explained how to manage the paddings of elements. This article focuses on the vertical and horizontal alignment of elements.

Alignment

The FrameworkElement has two alignment properties: HorizontalAlignment and VerticalAlignment. The HorizontalAlignment property is a type of HorizontalAlignment enumeration and represents how a child element is positioned within a parent element horizontally.

The HorizontalAlignment enumeration has the four properties Left, Center, Right and Stretch. The Left, Center and Right properties sets a child element to left, center and right of the parent element. The Stretch property stretches a child element to fill the parent element's allocated layout space.

Note: If the Width property is set to an element, Stretch alignment does not have any affect.

The code listed in Listing 8 places four Button elements on a StackPanel and sets the HorizontalAlignment property to Left, Center, Right and Stretch.

  1. <StackPanel Background="LightGray" >  
  2.    <Button Name="Rect1" Background="LightBlue"  
  3.       Width="150" Height="50"  
  4.       HorizontalAlignment="Left"  
  5.       Content="Left Aligned" />  
  6.   
  7.    <Button Name="Rect2" Background="LightGreen"  
  8.       Width="150" Height="50"  
  9.       HorizontalAlignment="Center"  
  10.       Content="Center Aligned" />  
  11.   
  12.    <Button Name="Rect3" Background="LightCyan"  
  13.       Width="150" Height="50"  
  14.       HorizontalAlignment="Right"  
  15.       Content="Right Aligned" />  
  16.   
  17.    <Button Name="Rect4" Background="LightPink"  
  18.       Height="50"  
  19.       HorizontalAlignment="Stretch"  
  20.       Content="Stretch Aligned" />  
  21. </StackPanel>  

Listing 1

Listing 1 generates output as in Figure 1. Now no matter how much you resize or move the window, the buttons will be aligned the same way.


                                                   Figure 1

The code listed in Listing 2 creates a StackPanel and adds four Button controls and sets their HorizontalAlignment property to Left, Center, Right and Stretch dynamically. The output looks as in exactly Figure 1.

  1. private void DynamicallyHorizontalAlignment()  
  2. {  
  3.    Button btn1 = new Button();  
  4.    btn1.Width = 150;  
  5.    btn1.Height = 50;  
  6.   
  7.    btn1.Background = new SolidColorBrush(Colors.LightBlue);  
  8.    btn1.Content = "Left Aligned";  
  9.    btn1.HorizontalAlignment = HorizontalAlignment.Left;  
  10.   
  11.    StackPanel1.Children.Add(btn1);  
  12.   
  13.    Button btn2 = new Button();  
  14.    btn2.Width = 150;  
  15.    btn2.Height = 50;  
  16.    btn2.Background = new SolidColorBrush(Colors.LightGreen);  
  17.    btn2.Content = "Center Aligned";  
  18.    btn2.HorizontalAlignment = HorizontalAlignment.Center;  
  19.    StackPanel1.Children.Add(btn2);  
  20.   
  21.    Button btn3 = new Button();  
  22.    btn3.Width = 150;  
  23.    btn3.Height = 50;  
  24.    btn3.Background = new SolidColorBrush(Colors.LightCyan);  
  25.    btn3.Content = "Right Aligned";  
  26.    btn3.HorizontalAlignment = HorizontalAlignment.Right;  
  27.    StackPanel1.Children.Add(btn3);  
  28.   
  29.    Button btn4 = new Button();  
  30.    btn4.Height = 50;  
  31.    btn4.Background = new SolidColorBrush(Colors.LightPink);  
  32.    btn4.Content = "Stretch Aligned";  
  33.    btn4.HorizontalAlignment = HorizontalAlignment.Stretch ;  
  34.    StackPanel1.Children.Add(btn4);  
  35.   
  36. }  

Listing 2

The VerticalAlignment property is a type of HorizontalAlignment enumeration and represents how a child element is positioned within a parent element vertically.

The VerticalAlignment enumeration has the four properties Top, Center, Bottom and Stretch. The Top, Center and Bottom properties set a child element to top, center or bottom of the parent element. The Stretch property stretches a child element to fill the parent element's allocated layout space vertically.

Note: If the Height property is set to an element, Stretch alignment does not have any affect.

The code listed in Listing 3 places four Button elements on a Grid element and set the VerticalAlignment property to Top, Center, Bottom and Stretch.

  1. <Grid>  
  2.    <Grid.ColumnDefinitions>  
  3.       <ColumnDefinition Width="100" />  
  4.       <ColumnDefinition Width="100" />  
  5.       <ColumnDefinition Width="100" />  
  6.       <ColumnDefinition Width="100" />  
  7.    </Grid.ColumnDefinitions>  
  8.   
  9.    <Button Name="Button1" Background="LightBlue"  
  10.       Height="30" Width="100"  
  11.       VerticalAlignment="Top"  
  12.       Content="Left Aligned" />  
  13.   
  14.    <Button Name="Button2" Background="LightGreen"  
  15.       Height="30" Width="100" Grid.Column="1"  
  16.       VerticalAlignment="Center"  
  17.       Content="Center Aligned" />  
  18.   
  19.    <Button Name="Button3" Background="LightCyan"  
  20.       VerticalAlignment="Bottom"  
  21.       Height="30" Width="100" Grid.Column="2"  
  22.       HorizontalAlignment="Left"  
  23.       Content="Right Aligned" />  
  24.   
  25.    <Button Name="Button4" Background="LightPink" Content="Stretch ligned"  
  26.       Width="100" Grid.Column="3"  
  27.       HorizontalAlignment="Stretch" />  
  28.   
  29. </Grid>  

Listing 3

Listing 3
generates output as in Figure 2. Now no matter how much you resize or move the window, the buttons will be aligned the same way.


                                             Figure 2

The code listed in Listing 4 creates a Grid and adds four Button controls and sets their VerticalAlignment property to Top, Center, Bottom and Stretch dynamically. The output looks as in exactly Figure 2.

  1. private void DynamicallyVerticalAlignment()  
  2. {  
  3.    Button btn1 = new Button();  
  4.    btn1.Width = 100;  
  5.    btn1.Height = 30;  
  6.    btn1.Background = new SolidColorBrush(Colors.LightBlue);  
  7.    btn1.Content = "Top Aligned";  
  8.    btn1.VerticalAlignment = VerticalAlignment.Top;  
  9.    Grid1.Children.Add(btn1);  
  10.   
  11.    Button btn2 = new Button();  
  12.    btn2.Width = 100;  
  13.    btn2.Height = 30;  
  14.    btn2.Background = new SolidColorBrush(Colors.LightGreen);  
  15.    btn2.Content = "Center Aligned";  
  16.    btn2.VerticalAlignment = VerticalAlignment.Center;  
  17.    Grid1.Children.Add(btn2);  
  18.   
  19.    Button btn3 = new Button();  
  20.    btn3.Width = 100;  
  21.    btn3.Height = 30;  
  22.    btn3.Background = new SolidColorBrush(Colors.LightCyan);  
  23.    btn3.Content = "Bottom Aligned";  
  24.    btn3.VerticalAlignment = VerticalAlignment.Bottom;  
  25.    Grid1.Children.Add(btn3);  
  26.   
  27.    Button btn4 = new Button();  
  28.    btn4.Width = 100;  
  29.    btn4.Background = new SolidColorBrush(Colors.LightPink);  
  30.    btn4.Content = "Stretch Aligned";  
  31.    btn4.VerticalAlignment = VerticalAlignment.Stretch;  
  32.    Grid1.Children.Add(btn4);  
  33.   
  34. }  

Listing 4

Summary


In this article, I discussed the vertical and horizontal alignment of WPF elements. The next article of this series explains the content alignment of WPF elements.

Up Next
    Ebook Download
    View all
    Learn
    View all