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)
Visual Basic (Declaration)
Public Class StackPanel _
Inherits Panel
Dim instance As StackPanel
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.
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. |
The following example shows how 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>
The preceding example produces output that is similar to the following illustration.
.png)
The following code example uses the Insert method to insert an element into an existing StackPanel control at a specified position.
<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>
Private Sub Rearrange()
Dim TB4 As New TextBlock()
TB4.Text = "Age"
MyStackPanel.Children.Insert(2, TB4)
End Sub
private void Rearrange()
{
TextBlock TB4 = new TextBlock();
TB4.Text = "Age";
MyStackPanel.Children.Insert(2, TB4);
}
System..::.Object
System.Windows..::.DependencyObject
System.Windows..::.UIElement
System.Windows..::.FrameworkElement
System.Windows.Controls..::.Panel
System.Windows.Controls..::.StackPanel
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Reference
Other Resources