StackPanel
The StackPanel arranges its child controls by stacking them horizontally or vertically. The stack panel is often used to arrange a small subsection of the UI on a page.
Inside a stack panel, if the size property perpendicular to the stack on a child control is not set, the child control will stretch to fill the available space. For example in horizontal orientation, the height of child controls will stretch if not set.
In the direction of the stack, the stack panel will always expand to fit all the child controls.
Useful Properties
You will probably use these properties most often:
| Property | Description |
|---|---|
Orientation | Sets the direction of the stack. Choose from horizontal or vertical (default). |
Spacing | Creates an even spacing between the child controls. |
Example
The following XAML shows how to create a vertical stack panel. The result shows the child controls stretched to fit the width, and the overall height of the stack panel equal to the sum of the child control heights.
- XAML
<StackPanel xmlns="https://github.com/avaloniaui" Width="200"> <Rectangle Fill="Red" Height="50"/> <Rectangle Fill="Blue" Height="50"/> <Rectangle Fill="Green" Height="50"/> <Rectangle Fill="Orange" Height="50"/> </StackPanel>
Defining a StackPanel in code
The following example demonstrates how to use a StackPanel to create a set of vertically-positioned buttons. For horizontal positioning, set the Orientation property to Horizontal.
- XAML
- C#
<StackPanel HorizontalAlignment="Center"
VerticalAlignment="Top"
Spacing="25">
<Button Content="Button 1" />
<Button Content="Button 2" />
<Button Content="Button 3" />
</StackPanel>
// Define the StackPanel
myStackPanel = new StackPanel();
myStackPanel.HorizontalAlignment = HorizontalAlignment.Center;
myStackPanel.VerticalAlignment = VerticalAlignment.Top;
myStackPanel.Spacing = 25;
// Define child content
Button myButton1 = new Button();
myButton1.Content = "Button 1";
Button myButton2 = new Button();
myButton2.Content = "Button 2";
Button myButton3 = new Button();
myButton3.Content = "Button 3";
// Add child elements to the parent StackPanel
myStackPanel.Children.Add(myButton1);
myStackPanel.Children.Add(myButton2);
myStackPanel.Children.Add(myButton3);
More Information
For the complete API documentation about this control, see here.
View the source code on GitHub StackPanel.cs
Have questions or feedback? Join the conversation below.