WPF: StackPanel

This entry is part 8 of 54 in the series Learn WPF

In our example, we have used a button and a textbox to display a message.

We can use a StackPanel control to arrange the Button and Textbox so that they occupy the top and the bottom of the page, each taking 50% of the space.


<Window x:Class="SayHello.MainWindow"
        Title="MainWindow" Height="350" Width="500">
        <TextBox  x:Name="textBox1" HorizontalAlignment="Stretch" Margin="50" Text="This is a text box." Height="60" />
        <Button HorizontalAlignment="Stretch" Margin="120,50" Click="Button_Click" Content="Say Hello"/>


In the Textbox control, we have used a margin of 50px. This will give a margin of 50px to the textbox on the left and the right-hand side of the text box.

For the Button, there are 2 values in the margin.  It will give a left margin of 120px and a top margin of 50px to the Button control.

Series Navigation<< WPF: Design the UI using designer
WPF: Grid >>