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);
}