Arranges child elements into a single line that can be oriented horizontally or vertically.
Namespace: System.Windows.Controls
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace: http://schemas.microsoft.com/winfx/2006/xaml/presentation
Visual Basic (Declaration)
Public Class StackPanel
Inherits Panel
Implements IScrollInfo
Dim instance As StackPanel
public class StackPanel : Panel, IScrollInfo
public ref class StackPanel : public Panel, IScrollInfo
public class StackPanel extends Panel implements IScrollInfo
public class StackPanel extends Panel implements IScrollInfo
XAML Object Element Usage
<StackPanel>
Children
</StackPanel>
Content Model: StackPanel enforces a strong content model for child content. See the Children property for more information about the Panel content model.
The default value is stretch for both HorizontalAlignment and VerticalAlignment of content that is contained in a StackPanel.
Panel elements do not receive focus by default. To compel a panel element to receive focus, set the Focusable property to true.
StackPanel implements the IScrollInfo interface to support logical scrolling. Logical scrolling is used to scroll to the next element in the logical tree. This is in contrast to physical scrolling, which scrolls content by a defined physical increment in a given direction. If you require physical scrolling instead of logical scrolling, wrap the host StackPanel element in a ScrollViewer and set its CanContentScroll property to false.
This example shows how to create a StackPanel.
A StackPanel allows you to stack elements in a specified direction. By using properties that are defined on StackPanel, content can flow both vertically, which is the default setting, or horizontally.
The following example vertically stacks five TextBlock controls, each with a different Border and Background, by using StackPanel. The child elements that have no specified Width stretch to fill the parent window; however, the child elements that have a specified Width, are centered within the window.
The default stack direction in a StackPanel is vertical. To control content flow in a StackPanel, use the Orientation property. You can control horizontal alignment by using the HorizontalAlignment property.
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowTitle="StackPanel Sample">
<StackPanel>
<Border Background="SkyBlue" BorderBrush="Black" BorderThickness="1">
<TextBlock Foreground="Black" FontSize="12">Stacked Item #1</TextBlock>
</Border>
<Border Width="400" Background="CadetBlue" BorderBrush="Black" BorderThickness="1">
<TextBlock Foreground="Black" FontSize="14">Stacked Item #2</TextBlock>
</Border>
<Border Background="LightGoldenRodYellow" BorderBrush="Black" BorderThickness="1">
<TextBlock Foreground="Black" FontSize="16">Stacked Item #3</TextBlock>
</Border>
<Border Width="200" Background="PaleGreen" BorderBrush="Black" BorderThickness="1">
<TextBlock Foreground="Black" FontSize="18">Stacked Item #4</TextBlock>
</Border>
<Border Background="White" BorderBrush="Black" BorderThickness="1">
<TextBlock Foreground="Black" FontSize="20">Stacked Item #5</TextBlock>
</Border>
</StackPanel>
</Page>
More Code
System.Object
System.Windows.Threading.DispatcherObject
System.Windows.DependencyObject
System.Windows.Media.Visual
System.Windows.UIElement
System.Windows.FrameworkElement
System.Windows.Controls.Panel
System.Windows.Controls.StackPanel
System.Windows.Controls.Primitives.ToolBarPanel
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
.NET Framework
Supported in: 3.0