Export (0) Print
Expand All
2 out of 11 rated this helpful - Rate this topic

StackPanel Class

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

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
public class StackPanel : Panel, IScrollInfo
<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

How to: Choose Between StackPanel and DockPanel This example shows how to choose between using a StackPanel or a DockPanel when you stack content in a Panel.
How to: Horizontally or Vertically Align Content in a StackPanel This example shows how to adjust the Orientation of content within a StackPanel element, and also how to adjust the HorizontalAlignment and VerticalAlignment of child content.
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 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.