Freigeben über


How to: Instantiate and Use a StackPanel in Code

Example

The following code example demonstrates how to instantiate and use a StackPanel using code. Five Border elements are added as Children of the StackPanel, each hosting a child TextBlock. The Border elements are stacked vertically, the default stacking orientation of a StackPanel.

// Create a StackPanel and Add children
StackPanel myStackPanel = new StackPanel();
Border myBorder1 = new Border();
myBorder1.Background = Brushes.SkyBlue;
myBorder1.BorderBrush = Brushes.Black;
myBorder1.BorderThickness = new Thickness(1);
TextBlock txt1 = new TextBlock();
txt1.Foreground = Brushes.Black;
txt1.FontSize = 12;
txt1.Text = "Stacked Item #1";
myBorder1.Child = txt1;

Border myBorder2 = new Border();
myBorder2.Background = Brushes.CadetBlue;
myBorder2.Width = 400;
myBorder2.BorderBrush = Brushes.Black;
myBorder2.BorderThickness = new Thickness(1);
TextBlock txt2 = new TextBlock();
txt2.Foreground = Brushes.Black;
txt2.FontSize = 14;
txt2.Text = "Stacked Item #2";
myBorder2.Child = txt2;

Border myBorder3 = new Border();
myBorder3.Background = Brushes.LightGoldenrodYellow;
myBorder3.BorderBrush = Brushes.Black;
myBorder3.BorderThickness = new Thickness(1);
TextBlock txt3 = new TextBlock();
txt3.Foreground = Brushes.Black;
txt3.FontSize = 16;
txt3.Text = "Stacked Item #3";
myBorder3.Child = txt3;

Border myBorder4 = new Border();
myBorder4.Background = Brushes.PaleGreen;
myBorder4.Width = 200;
myBorder4.BorderBrush = Brushes.Black;
myBorder4.BorderThickness = new Thickness(1);
TextBlock txt4 = new TextBlock();
txt4.Foreground = Brushes.Black;
txt4.FontSize = 18;
txt4.Text = "Stacked Item #4";
myBorder4.Child = txt4;

Border myBorder5 = new Border();
myBorder5.Background = Brushes.White;
myBorder5.BorderBrush = Brushes.Black;
myBorder5.BorderThickness = new Thickness(1);
TextBlock txt5 = new TextBlock();
txt5.Foreground = Brushes.Black;
txt5.FontSize = 20;
txt5.Text = "Stacked Item #5";
myBorder5.Child = txt5;

// Add the Borders to the StackPanel Children Collection
myStackPanel.Children.Add(myBorder1);
myStackPanel.Children.Add(myBorder2);
myStackPanel.Children.Add(myBorder3);
myStackPanel.Children.Add(myBorder4);
myStackPanel.Children.Add(myBorder5);
mainWindow.Content = myStackPanel;
WindowTitle = "StackPanel Sample"
'Create a StackPanel as the root Panel
Dim myStackPanel As New StackPanel()
Dim myBorder1 As New Border()
myBorder1.Background = Brushes.SkyBlue
myBorder1.BorderBrush = Brushes.Black
myBorder1.BorderThickness = New Thickness(1)
Dim txt1 As New TextBlock()
txt1.Foreground = Brushes.Black
txt1.FontSize = 12
txt1.Text = "Stacked Item #1"
myBorder1.Child = txt1

Dim myBorder2 As New Border()
myBorder2.Background = Brushes.CadetBlue
myBorder2.Width = 400
myBorder2.BorderBrush = Brushes.Black
myBorder2.BorderThickness = New Thickness(1)
Dim txt2 As New TextBlock()
txt2.Foreground = Brushes.Black
txt2.FontSize = 14
txt2.Text = "Stacked Item #2"
myBorder2.Child = txt2

Dim myBorder3 As New Border()
myBorder3.Background = Brushes.LightGoldenrodYellow
myBorder3.BorderBrush = Brushes.Black
myBorder3.BorderThickness = New Thickness(1)
Dim txt3 As New TextBlock()
txt3.Foreground = Brushes.Black
txt3.FontSize = 16
txt3.Text = "Stacked Item #3"
myBorder3.Child = txt3

Dim myBorder4 As New Border()
myBorder4.Background = Brushes.PaleGreen
myBorder4.Width = 200
myBorder4.BorderBrush = Brushes.Black
myBorder4.BorderThickness = New Thickness(1)
Dim txt4 As New TextBlock()
txt4.Foreground = Brushes.Black
txt4.FontSize = 18
txt4.Text = "Stacked Item #4"
myBorder4.Child = txt4

Dim myBorder5 As New Border()
myBorder5.Background = Brushes.White
myBorder5.BorderBrush = Brushes.Black
myBorder5.BorderThickness = New Thickness(1)
Dim txt5 As New TextBlock()
txt5.Foreground = Brushes.Black
txt5.FontSize = 20
txt5.Text = "Stacked Item #5"
myBorder5.Child = txt5

' Add the Borders to the StackPanel Children Collection
myStackPanel.Children.Add(myBorder1)
myStackPanel.Children.Add(myBorder2)
myStackPanel.Children.Add(myBorder3)
myStackPanel.Children.Add(myBorder4)
myStackPanel.Children.Add(myBorder5)
Me.Content = myStackPanel

See Also

Reference

StackPanel

Concepts

Panels Overview