System.Windows.Controls Nam ...


.NET Framework Class Library for Silverlight
StackPanel Class

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

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)
Syntax

Visual Basic (Declaration)
Public Class StackPanel _
    Inherits Panel
Visual Basic (Usage)
Dim instance As StackPanel
C#
public class StackPanel : Panel
XAML Object Element Usage
<StackPanel .../>
-or-
<StackPanel ...>
  oneOrMoreChildren
</StackPanel>

XAML Values

oneOrMoreChildren

One or more object elements of types that derive from UIElement, which are contained by this StackPanel. See Children.

Remarks

StackPanel is one of the Panel elements that enable layout. StackPanel is useful for the specific scenario where you want to arrange a set of objects in a vertical or horizontal list (for example, a horizontal or vertical menu of items). Set the Orientation property to determine the direction of the list. The default value for the Orientation property is Vertical.

The default for both HorizontalAlignment and VerticalAlignment of content in a StackPanel is Stretch.

The following table summarizes the other available layout containers provided by Silverlight.

Panel name

Description

Canvas

Defines an area that you can explicitly position child elements.

Grid

Defines a flexible grid area consisting of columns and rows.

Examples

The following example shows how create a StackPanel of items.

XAML
<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>

The preceding example produces output that is similar to the following illustration.

ms612971.stackpanel_verticle_basic(en-us,VS.95).png

The following code example uses the Insert method to insert an element into an existing StackPanel control at a specified position.

XAML
<Grid x:Name="LayoutRoot" Background="White" 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>
Visual Basic
Private Sub Rearrange()
    Dim TB4 As New TextBlock()
    TB4.Text = "Age"
    MyStackPanel.Children.Insert(2, TB4)
End Sub
C#
private void Rearrange()
{
    TextBlock TB4 = new TextBlock();
    TB4.Text = "Age";
    MyStackPanel.Children.Insert(2, TB4);

}
Inheritance Hierarchy

System..::.Object
  System.Windows..::.DependencyObject
    System.Windows..::.UIElement
      System.Windows..::.FrameworkElement
        System.Windows.Controls..::.Panel
          System.Windows.Controls..::.StackPanel
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

Other Resources

Tags :


Page view tracker