StackPanel Control
The StackPanel
is a layout control that arranges child elements in a single line, either horizontally or vertically. It's a fundamental building block for creating user interfaces in Windows applications using the Windows UI Library (WinUI).
Key Properties
- Orientation: Determines whether child elements are stacked horizontally or vertically. Can be set to
Horizontal
orVertical
(default isVertical
). - Spacing: Adds space between child elements.
Basic Usage
Here's a simple example of a vertical StackPanel
containing a few elements:
XAML Example
<StackPanel Orientation="Vertical" Spacing="10">
<Button Content="Button 1" />
<TextBlock Text="Some Text" />
<TextBox PlaceholderText="Enter something" />
</StackPanel>
Horizontal Orientation
You can easily switch the orientation to horizontal:
XAML Example (Horizontal)
<StackPanel Orientation="Horizontal" Spacing="15">
<Button Content="First" />
<TextBlock Text="Second" />
<Image Source="ms-appx:///Assets/icon.png" Width="30" Height="30" />
</StackPanel>
Demonstration
Live Demo
Item B
Item E
When to Use StackPanel
StackPanel
is ideal for:
- Arranging a small number of controls in a simple linear fashion.
- Creating rows or columns of related items.
- Situations where you need consistent spacing between elements.
For more complex layouts that require precise alignment, sizing, and arrangement of elements in both rows and columns, consider using the Grid
control.