Working With StackPanel In WPF Using C#

StackPanel is used to arrange child elements into 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 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 child elements on a StackPanel do not fit in the StackPanel area, they go outside of the visible area. If you wish 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="stackpanel-in-wpf" Height="stackpanel-in-wpf" 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.

StackPanel In WPF Using C#
Figure 1

Now in Listing 1, let's replace the Canvas with a StackPanel. The new code looks like 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 like Figure 2, where you can see all elements are stacked in vertical direction.

Stack in vertical direction
Figure 2

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

  1. <StackPanel Orientation="Horizontal" >  

The new output looks like Figure 3.

Orientation property to horizontal
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. privatevoid CreateDynamicStackPanel()   
  2. {  
  3.     // Create a StackPanel and set its properties  
  4.     StackPanel dynamicStackPanel = newStackPanel();  
  5.     dynamicStackPanel.Width = 300;  
  6.     dynamicStackPanel.Height = 200;  
  7.     dynamicStackPanel.Background = newSolidColorBrush(Colors.LightBlue);  
  8.     dynamicStackPanel.Orientation = Orientation.Horizontal;  
  9.     // Create Ellipses and add to StackPanel  
  10.     Ellipse redCircle = newEllipse();  
  11.     redCircle.Width = 100;  
  12.     redCircle.Height = 100;  
  13.     redCircle.Fill = newSolidColorBrush(Colors.Red);  
  14.     dynamicStackPanel.Children.Add(redCircle);  
  15.     Ellipse orangeCircle = newEllipse();  
  16.     orangeCircle.Width = 80;  
  17.     orangeCircle.Height = 80;  
  18.     orangeCircle.Fill = newSolidColorBrush(Colors.Orange);  
  19.     dynamicStackPanel.Children.Add(orangeCircle);  
  20.     Ellipse yellowCircle = newEllipse();  
  21.     yellowCircle.Width = 60;  
  22.     yellowCircle.Height = 60;  
  23.     yellowCircle.Fill = newSolidColorBrush(Colors.Yellow);  
  24.     dynamicStackPanel.Children.Add(yellowCircle);  
  25.     Ellipse greenCircle = newEllipse();  
  26.     greenCircle.Width = 40;  
  27.     greenCircle.Height = 40;  
  28.     greenCircle.Fill = newSolidColorBrush(Colors.Green);  
  29.     dynamicStackPanel.Children.Add(greenCircle);  
  30.     Ellipse blueCircle = newEllipse();  
  31.     blueCircle.Width = 20;  
  32.     blueCircle.Height = 20;  
  33.     blueCircle.Fill = newSolidColorBrush(Colors.Blue);  
  34.     dynamicStackPanel.Children.Add(blueCircle);  
  35.     // Display StackPanel into a Window  
  36.     RootWindow.Content = dynamicStackPanel;  
  37. }  

Listing 3

The output of Listing 3 generates Figure 4.

StackPanel In WPF Using C#
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.

How to Fill a Windows with StackPanel

If you need to fill a parent control or Window with a panel or scale to fit width or height or both, you use the VerticalAlignment,  HorizontalAlignment, Width, and Height properties of the StackPanel.
  1. <Grid>  
  2.         <StackPanel Margin="0,0,0,0" VerticalAlignment="Stretch"   
  3.         HorizontalAlignment="Stretch"   
  4.         Width="Auto" Height="Auto"  
  5.         Background="#FFE44343"/>  
  6. </Grid>  


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.