WPF Layout: StackPanel

Proper layout and positioning are a vital part of interactive, high-performance and user-friendly Windows applications. This article of this series of articles explains the StackPanel 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: GridPanel, I discussed the GridPanel. This article focuses on the StackPanel in details.

StackPanel

A StackPanel arranges child elements in a single line that can be oriented horizontally or vertically. The StackPanel element in XAML represents a StackPanel. The following code snippet creates a StackPanel at design-time using XAML.

  1. <StackPanel Width="300" Height="200" Background="LightBlue" />  
The Orientation property represents the direction of the children that can be vertical or horizontal. The default property of Orientation is vertical. The following code snippet sets the Orientation property to Horizontal.
  1. <StackPanel Width="300" Height="200" Background="LightBlue" Orientation="Horizontal" />  
Note: If the child elements on a StackPanel do not fit in the StackPanel area, they overflow into the visible area. If you need to wrap the child elements when they are outside of the StackPanel area, use WrapPanel instead.

Let's see an example. The code listed in Listing 1 places five ellipse elements in a Canvas.
  1. <Canvas >   
  2.    <Ellipse Width="100" Height="100" Fill="Red" />   
  3.    <Ellipse Width="80" Height="80" Fill="Orange" />   
  4.    <Ellipse Width="60" Height="60" Fill="Yellow" />   
  5.    <Ellipse Width="40" Height="40" Fill="Green" />   
  6.    <Ellipse Width="20" Height="20" Fill="Blue" />   
  7. </Canvas>  

Listing 1

Listing 1 generates Figure 1.



Figure 1

Now in Listing 2, let's replace Canvas with a StackPanel. The new code looks as in Listing 2.

  1. <StackPanel >   
  2.    <Ellipse Width="100" Height="100" Fill="Red" />   
  3.    <Ellipse Width="80" Height="80" Fill="Orange" />   
  4.    <Ellipse Width="60" Height="60" Fill="Yellow" />   
  5.    <Ellipse Width="40" Height="40" Fill="Green" />   
  6.    <Ellipse Width="20" Height="20" Fill="Blue" />   
  7. </StackPanel>  

Listing 2

The new output looks as in Figure 2, where you can see all the elements are stacked in the vertical direction.



Figure 2

Now let's change the Orientation property to horizontal by changing the StackPanel code to the following.

  1. <StackPanel Orientation="Horizontal" >  
The new output looks as in Figure 3.



Figure 3

The StackPanel class in WPF represents a StackPanel. The code listed in Listing 3 creates a StackPanel dynamically, sets its properties and adds five ellipses.

  1. private void CreateDynamicStackPanel()   
  2. {  
  3.     // Create a StackPanel and set its properties   
  4.     StackPanel dynamicStackPanel = new StackPanel();  
  5.     dynamicStackPanel.Width = 300;  
  6.     dynamicStackPanel.Height = 200;  
  7.     dynamicStackPanel.Background = new SolidColorBrush(Colors.LightBlue);  
  8.     dynamicStackPanel.Orientation = Orientation.Horizontal;  
  9.   
  10.     // Create Ellipses and add to StackPanel   
  11.     Ellipse redCircle = new Ellipse();  
  12.     redCircle.Width = 100;  
  13.     redCircle.Height = 100;  
  14.     redCircle.Fill = new SolidColorBrush(Colors.Red);  
  15.     dynamicStackPanel.Children.Add(redCircle);  
  16.   
  17.     Ellipse orangeCircle = new Ellipse();  
  18.     orangeCircle.Width = 80;  
  19.     orangeCircle.Height = 80;  
  20.     orangeCircle.Fill = new SolidColorBrush(Colors.Orange);  
  21.     dynamicStackPanel.Children.Add(orangeCircle);  
  22.   
  23.     Ellipse yellowCircle = new Ellipse();  
  24.     yellowCircle.Width = 60;  
  25.     yellowCircle.Height = 60;  
  26.     yellowCircle.Fill = new SolidColorBrush(Colors.Yellow);  
  27.     dynamicStackPanel.Children.Add(yellowCircle);  
  28.   
  29.     Ellipse greenCircle = new Ellipse();  
  30.     greenCircle.Width = 40;  
  31.     greenCircle.Height = 40;  
  32.     greenCircle.Fill = new SolidColorBrush(Colors.Green);  
  33.     dynamicStackPanel.Children.Add(greenCircle);  
  34.   
  35.     Ellipse blueCircle = new Ellipse();  
  36.     blueCircle.Width = 20;  
  37.     blueCircle.Height = 20;  
  38.     blueCircle.Fill = new SolidColorBrush(Colors.Blue);  
  39.     dynamicStackPanel.Children.Add(blueCircle);  
  40.   
  41.     // Display StackPanel into a Window   
  42.     RootWindow.Content = dynamicStackPanel;  
  43. }  

Listing 3

The output of Listing 3 generates Figure 4.



Figure 4

When there is not enough space on a StackPanel vertically or horizontally, you may add a scrolling feature to a StackPanel. The CanHorizontallyScroll and CanVerticallyScroll properties are used to add scrolling functionality to a StackPanel.

Summary

This article explained the StackPanel in WPF. In the next article of this series, I will focus on the WrapPanel in details.

Up Next
    Ebook Download
    View all
    Learn
    View all