Windows.UI.Xaml.Controls.StackPanel

August 8, 2022 · View on GitHub

-description

Arranges child elements into a single line that can be oriented horizontally or vertically.

-xaml-syntax

<StackPanel .../>
-or-
<StackPanel ...>
  oneOrMoreChildren
</StackPanel>

-remarks

Tip

For more info, design guidance, and code examples, see Layout panels.

StackPanel is a layout panel that arranges child elements into a single line that can be oriented horizontally or vertically.

:::image type="content" source="images/controls/StackPanel.png" alt-text="StackPanel layout control":::

By default, StackPanel stacks items vertically from top to bottom in the order they are declared. You can set the Orientation property to Horizontal to stack items from left to right.

You can insert items into a StackPanel at a specific location using the InsertAt method in code-behind.

Border properties

StackPanel defines border properties that let you draw a border around the StackPanel without using an additional Border element. The properties are StackPanel.BorderBrush, StackPanel.BorderThickness, StackPanel.CornerRadius, and StackPanel.Padding.

<StackPanel BorderBrush="Red" BorderThickness="2" CornerRadius="10" Padding="12">
    <TextBlock Text="Hello World!"/>
</StackPanel>

Version history

Windows versionSDK versionValue added
160714393GetInsertionIndexes
170916299Spacing
180917763BackgroundSizing

-examples

Tip

For more info, design guidance, and code examples, see Stack panel.

[!div class="nextstepaction"] Open the WinUI 2 Gallery app and see the StackPanel in action

The WinUI 2 Gallery app includes interactive examples of most WinUI 2 controls, features, and functionality. Get the app from the Microsoft Store or get the source code on GitHub.

The following example shows how to create a StackPanel of items.

[!code-xamlLayoutOvwStackPanel_1]

[!code-xaml02]

[!code-csharp01]

[!code-vb01]

-see-also

Panel, IScrollSnapPointsInfo, Layout panels, Define layouts, Alignment, margin, and padding, Canvas, Grid, ItemsStackPanel, VariableSizedWrapGrid, VirtualizingStackPanel, Controls list, Controls by function