StackPanel StackPanel StackPanel Class

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

Syntax

Declaration

public class StackPanelpublic class StackPanelPublic Class StackPanel
<StackPanel .../>
-or-
<StackPanel ...>
  oneOrMoreChildren
</StackPanel>

Inheritance Hierarchy

Inherited Members

Inherited properties

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
Tag
Tag
Tag
, , , , , , , , , , , , , , , , , , , , , , , , ,

Inherited events

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Inherited methods

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Remarks

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

Stack panel 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(UInt32, UIElement) method in code-behind.

Tip

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

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

Examples

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

<StackPanel Margin="20">
    <Rectangle Fill="Red" Width="50" Height="50" Margin="5" />
    <Rectangle Fill="Blue" Width="50" Height="50" Margin="5" />
    <Rectangle Fill="Green" Width="50" Height="50" Margin="5" />
    <Rectangle Fill="Purple" Width="50" Height="50" Margin="5" />
</StackPanel>
<Grid x:Name="LayoutRoot"  Width="500" Height="500">
    <StackPanel x:Name="MyStackPanel">
        <TextBlock x:Name="TB1" Text="First Name" Width="77" HorizontalAlignment="Left"/>
        <TextBlock x:Name="TB2" Text="Last Name" Width="78" HorizontalAlignment="Left"/>
        <TextBlock x:Name="TB3" Text="Address" Width="60" HorizontalAlignment="Left"/>
    </StackPanel>
</Grid>
private void Rearrange()
{
    TextBlock TB4 = new TextBlock();
    TB4.Text = "Age";
    MyStackPanel.Children.Insert(2, TB4);
    
}
Private Sub Rearrange()
    Dim TB4 As New TextBlock()
    TB4.Text = "Age"
    MyStackPanel.Children.Insert(2, TB4)
End Sub

Constructors summary

Initializes a new instance of the StackPanel class.

Properties summary

Gets a value that indicates whether the horizontal snap points for the StackPanel are equidistant from each other.

Gets or sets a value that indicates whether the generated snap points used for panning in the StackPanel are equidistant from each other.

Identifies the AreScrollSnapPointsRegular dependency property.

Gets a value that indicates whether the vertical snap points for the StackPanel are equidistant from each other.

Gets or sets a brush that describes the border fill of the panel.

Identifies the BorderBrush dependency property.

Gets or sets the border thickness of the panel.

Identifies the BorderThickness dependency property.

Gets or sets the radius for the corners of the panel's border.

Identifies the CornerRadius dependency property.

Gets or sets the dimension by which child elements are stacked.

Identifies the Orientation dependency property.

Gets or sets the distance between the border and its child object.

Identifies the Padding dependency property.

Methods summary

Returns the index values of the items that the specified point is between.

Returns the set of distances between irregular snap points for a specified orientation and alignment.

Gets the distance between regular snap points for a specified orientation and alignment.

Events summary

Occurs when the measurements for horizontal snap points change.

Occurs when the measurements for vertical snap points change.

Constructors

  • StackPanel()
    StackPanel()
    StackPanel()
    StackPanel()

    Initializes a new instance of the StackPanel class.

    public StackPanel()public New()Public Sub New()public StackPanel()

Properties

  • AreHorizontalSnapPointsRegular
    AreHorizontalSnapPointsRegular
    AreHorizontalSnapPointsRegular
    AreHorizontalSnapPointsRegular

    Gets a value that indicates whether the horizontal snap points for the StackPanel are equidistant from each other.

    public bool AreHorizontalSnapPointsRegular { get; }public bool AreHorizontalSnapPointsRegular { get; }Public ReadOnly Property AreHorizontalSnapPointsRegular As boolpublic bool AreHorizontalSnapPointsRegular { get; }

    Property Value

    • bool
      bool
      bool

      true if the horizontal snap points for the StackPanel are equidistant from each other; otherwise, false.

  • AreScrollSnapPointsRegular
    AreScrollSnapPointsRegular
    AreScrollSnapPointsRegular
    AreScrollSnapPointsRegular

    Gets or sets a value that indicates whether the generated snap points used for panning in the StackPanel are equidistant from each other.

    public bool AreScrollSnapPointsRegular { get; set; }public bool AreScrollSnapPointsRegular { get; set; }Public ReadWrite Property AreScrollSnapPointsRegular As boolpublic bool AreScrollSnapPointsRegular { get; set; }
    <StackPanel AreScrollSnapPointsRegular="bool" .../>
    

    Property Value

    • bool
      bool
      bool

      true if the snap points in the StackPanel are equidistant from each other; otherwise, false.

  • AreScrollSnapPointsRegularProperty
    AreScrollSnapPointsRegularProperty
    AreScrollSnapPointsRegularProperty
    AreScrollSnapPointsRegularProperty

    Identifies the AreScrollSnapPointsRegular dependency property.

    public static DependencyProperty AreScrollSnapPointsRegularProperty { get; }public static DependencyProperty AreScrollSnapPointsRegularProperty { get; }Public Static ReadOnly Property AreScrollSnapPointsRegularProperty As DependencyPropertypublic static DependencyProperty AreScrollSnapPointsRegularProperty { get; }

    Property Value

  • AreVerticalSnapPointsRegular
    AreVerticalSnapPointsRegular
    AreVerticalSnapPointsRegular
    AreVerticalSnapPointsRegular

    Gets a value that indicates whether the vertical snap points for the StackPanel are equidistant from each other.

    public bool AreVerticalSnapPointsRegular { get; }public bool AreVerticalSnapPointsRegular { get; }Public ReadOnly Property AreVerticalSnapPointsRegular As boolpublic bool AreVerticalSnapPointsRegular { get; }

    Property Value

    • bool
      bool
      bool

      true if the vertical snap points for the StackPanel are equidistant from each other; otherwise, false.

  • BorderBrush
    BorderBrush
    BorderBrush
    BorderBrush

    Gets or sets a brush that describes the border fill of the panel.

    public Brush BorderBrush { get; set; }public Brush BorderBrush { get; set; }Public ReadWrite Property BorderBrush As Brushpublic Brush BorderBrush { get; set; }
    <Border BorderBrush="{StaticResource resourceName}"/>
    
    

    Property Value

    • The brush that is used to fill the panel's border. The default is null, (a null brush) which is evaluated as Transparent for rendering.

    Remarks

    The BorderThickness value must be greater than 0 in order to see the BorderBrush value take effect.

  • BorderBrushProperty
    BorderBrushProperty
    BorderBrushProperty
    BorderBrushProperty

    Identifies the BorderBrush dependency property.

    public static DependencyProperty BorderBrushProperty { get; }public static DependencyProperty BorderBrushProperty { get; }Public Static ReadOnly Property BorderBrushProperty As DependencyPropertypublic static DependencyProperty BorderBrushProperty { get; }

    Property Value

  • BorderThickness
    BorderThickness
    BorderThickness
    BorderThickness

    Gets or sets the border thickness of the panel.

    public Thickness BorderThickness { get; set; }public Thickness BorderThickness { get; set; }Public ReadWrite Property BorderThickness As Thicknesspublic Thickness BorderThickness { get; set; }
    <StackPanel BorderThickness="uniform"/>
    - or -
    <StackPanel BorderThickness="left&right,top&bottom"/>
    - or -
    <StackPanel BorderThickness="left,top,right,bottom"/>
    

    Property Value

  • BorderThicknessProperty
    BorderThicknessProperty
    BorderThicknessProperty
    BorderThicknessProperty

    Identifies the BorderThickness dependency property.

    public static DependencyProperty BorderThicknessProperty { get; }public static DependencyProperty BorderThicknessProperty { get; }Public Static ReadOnly Property BorderThicknessProperty As DependencyPropertypublic static DependencyProperty BorderThicknessProperty { get; }

    Property Value

  • CornerRadius
    CornerRadius
    CornerRadius
    CornerRadius

    Gets or sets the radius for the corners of the panel's border.

    public CornerRadius CornerRadius { get; set; }public CornerRadius CornerRadius { get; set; }Public ReadWrite Property CornerRadius As CornerRadiuspublic CornerRadius CornerRadius { get; set; }
    <StackPanel CornerRadius="uniformRadius"/>
    - or -
    <StackPanel CornerRadius="topLeft,topRight,bottomRight,bottomLeft"/>
    
    

    Property Value

    Remarks

    Member components of a CornerRadius value cannot be negative.

  • CornerRadiusProperty
    CornerRadiusProperty
    CornerRadiusProperty
    CornerRadiusProperty

    Identifies the CornerRadius dependency property.

    public static DependencyProperty CornerRadiusProperty { get; }public static DependencyProperty CornerRadiusProperty { get; }Public Static ReadOnly Property CornerRadiusProperty As DependencyPropertypublic static DependencyProperty CornerRadiusProperty { get; }

    Property Value

  • Orientation
    Orientation
    Orientation
    Orientation

    Gets or sets the dimension by which child elements are stacked.

    public Orientation Orientation { get; set; }public Orientation Orientation { get; set; }Public ReadWrite Property Orientation As Orientationpublic Orientation Orientation { get; set; }
    <StackPanel Orientation="Horizontal"/>
    -or-
    <StackPanel Orientation="Vertical"/>
    

    Property Value

    Remarks

    Examples

    The following example creates a horizontal list of items by setting the Orientation property to Horizontal.

    <StackPanel Orientation="Horizontal" Margin="20">
        <Rectangle Fill="Red" Width="50" Height="50" Margin="5" />
        <Rectangle Fill="Blue" Width="50" Height="50" Margin="5" />
        <Rectangle Fill="Green" Width="50" Height="50" Margin="5" />
        <Rectangle Fill="Purple" Width="50" Height="50" Margin="5" />
    </StackPanel>
    
    -->
    
  • OrientationProperty
    OrientationProperty
    OrientationProperty
    OrientationProperty

    Identifies the Orientation dependency property.

    public static DependencyProperty OrientationProperty { get; }public static DependencyProperty OrientationProperty { get; }Public Static ReadOnly Property OrientationProperty As DependencyPropertypublic static DependencyProperty OrientationProperty { get; }

    Property Value

  • Padding
    Padding
    Padding
    Padding

    Gets or sets the distance between the border and its child object.

    public Thickness Padding { get; set; }public Thickness Padding { get; set; }Public ReadWrite Property Padding As Thicknesspublic Thickness Padding { get; set; }

    Property Value

    Remarks

    A related property is Margin (a property of FrameworkElement ). For more info about the relationship between margin and padding, see Alignment, margin, and padding or Define layouts with XAML.

  • PaddingProperty
    PaddingProperty
    PaddingProperty
    PaddingProperty

    Identifies the Padding dependency property.

    public static DependencyProperty PaddingProperty { get; }public static DependencyProperty PaddingProperty { get; }Public Static ReadOnly Property PaddingProperty As DependencyPropertypublic static DependencyProperty PaddingProperty { get; }

    Property Value

Methods

  • GetInsertionIndexes(Point, Int32, Int32)
    GetInsertionIndexes(Point, Int32, Int32)
    GetInsertionIndexes(Point, Int32, Int32)
    GetInsertionIndexes(Point, Int32, Int32)

    Returns the index values of the items that the specified point is between.

    public void GetInsertionIndexes(Point position, Int32 first, Int32 second)public void GetInsertionIndexes(Point position, Int32 first, Int32 second)Public Function GetInsertionIndexes(position As Point, first As Int32, second As Int32) As voidpublic void GetInsertionIndexes(Point position, Int32 first, Int32 second)

    Parameters

    • position

      The point for which to get insertion indexes.

    • first
      System.Int32
      System.Int32
      System.Int32

      The index of the item before the specified point.

    • second
      System.Int32
      System.Int32
      System.Int32

      The index of the item after the specified point.

    Remarks

    Call this method when handling a DragOver event to return the indices of the two items between which the DragOver is happening and where a potential drop and insertion would happen.

    Version compatibility

    The GetInsertionIndexes(Point, Int32, Int32) method is not available prior to Windows 10, version 1607. If your app’s 'minimum platform version' setting in Microsoft Visual Studio is less than the 'introduced version' shown in the Requirements block later in this page, you must design and test your app to account for this. For more info, see Version adaptive code.

    To avoid exceptions when your app runs on previous versions of Windows 10, do not call this method without first performing a runtime check. This example shows how to use the ApiInformation class to check for the presence of this method before you use it.

    <StackPanel AllowDrop="True" DragOver="StackPanel_DragOver">
    
    private void StackPanel_DragOver(object sender, DragEventArgs e)
    {
        if (ApiInformation.IsMethodPresent("Windows.UI.Xaml.Controls.StackPanel", "GetInsertionIndexes"))
        {
            StackPanel stackPanel = sender as StackPanel;
            if (stackPanel != null)
            {
                int preceedingIndex;
                int subsequentIndex;
                stackPanel.GetInsertionIndexes(e.GetPosition(stackPanel), out preceedingIndex, out subsequentIndex);
            }
        }
    }
    
  • GetIrregularSnapPoints(Orientation, SnapPointsAlignment)
    GetIrregularSnapPoints(Orientation, SnapPointsAlignment)
    GetIrregularSnapPoints(Orientation, SnapPointsAlignment)
    GetIrregularSnapPoints(Orientation, SnapPointsAlignment)

    Returns the set of distances between irregular snap points for a specified orientation and alignment.

    public IVectorView<float> GetIrregularSnapPoints(Orientation orientation, SnapPointsAlignment alignment)public IVectorView<float> GetIrregularSnapPoints(Orientation orientation, SnapPointsAlignment alignment)Public Function GetIrregularSnapPoints(orientation As Orientation, alignment As SnapPointsAlignment) As IVectorView( Of float )public IVectorView<float> GetIrregularSnapPoints(Orientation orientation, SnapPointsAlignment alignment)

    Parameters

    Returns

    • The read only collection of snap point distances. Returns an empty collection when no snap points are present.

  • GetRegularSnapPoints(Orientation, SnapPointsAlignment, Single)
    GetRegularSnapPoints(Orientation, SnapPointsAlignment, Single)
    GetRegularSnapPoints(Orientation, SnapPointsAlignment, Single)
    GetRegularSnapPoints(Orientation, SnapPointsAlignment, Single)

    Gets the distance between regular snap points for a specified orientation and alignment.

    public float GetRegularSnapPoints(Orientation orientation, SnapPointsAlignment alignment, Single offset)public float GetRegularSnapPoints(Orientation orientation, SnapPointsAlignment alignment, Single offset)Public Function GetRegularSnapPoints(orientation As Orientation, alignment As SnapPointsAlignment, offset As Single) As floatpublic float GetRegularSnapPoints(Orientation orientation, SnapPointsAlignment alignment, Single offset)

    Parameters

    Returns

    • float
      float
      float

      The distance between the equidistant snap points. Returns 0 when no snap points are present.

Events

  • HorizontalSnapPointsChanged
    HorizontalSnapPointsChanged
    HorizontalSnapPointsChanged
    HorizontalSnapPointsChanged

    Occurs when the measurements for horizontal snap points change.

    public event EventHandler HorizontalSnapPointsChangedpublic event EventHandler HorizontalSnapPointsChangedPublic Event HorizontalSnapPointsChangedpublic event EventHandler HorizontalSnapPointsChanged
    <StackPanel HorizontalSnapPointsChanged="eventhandler" />
    
  • VerticalSnapPointsChanged
    VerticalSnapPointsChanged
    VerticalSnapPointsChanged
    VerticalSnapPointsChanged

    Occurs when the measurements for vertical snap points change.

    public event EventHandler VerticalSnapPointsChangedpublic event EventHandler VerticalSnapPointsChangedPublic Event VerticalSnapPointsChangedpublic event EventHandler VerticalSnapPointsChanged
    <StackPanel VerticalSnapPointsChanged="eventhandler" />
    

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.UniversalApiContract (introduced v1)

Attributes

Windows.Foundation.Metadata.ComposableAttribute
Windows.Foundation.Metadata.ContractVersionAttribute
Windows.Foundation.Metadata.MarshalingBehaviorAttribute
Windows.Foundation.Metadata.StaticAttribute
Windows.Foundation.Metadata.StaticAttribute
Windows.Foundation.Metadata.ThreadingAttribute
Windows.Foundation.Metadata.WebHostHiddenAttribute

Details

Assembly

Windows.UI.Xaml.Controls.dll