Home > DeveloperSection > Beginner > StackPanel Control in WPF

StackPanel Control in WPF


WPF WPF 
Ratings:
0 Comment(s)
 5745  View(s)
Rate this:

StackPanel Control in WPF

In WPF StackPanel control is also used to group controls either horizontally or vertically. We can use StackPanel control to arrange child elements into a single line that is either horizontally or vertically. The <StackPanel  /> element in XAML is used to represent a StackPanel.

The following code snippet represents use of StackPanel in WPF

<StackPanel x:Name="panel1" Background="LightGoldenrodYellow" Width="300" Height="200">

            <Button x:Name="btnPanelDemo" Width="250" Height="30" Margin="10"  Content="StackPanel With Button Control" />

</StackPanel>

 

Output of the following code snippet is as follows

StackPanel Control in WPF

The orientation property of StackPanel element represents direction of the child elements in stack panel that is either horizontally or vertically. The following code snippets represent use of Orientation property in StackPanel.

<StackPanel x:Name="panel1" Background="LightGoldenrodYellow" Width="300" Orientation="Horizontal" Height="200">

            <Button x:Name="btnPanelDemo1" Width="130" Height="30" Margin="10" Content="Horizontal Orientation" />

            <Button x:Name="btnPanelDemo2" Width="130" Height="30" Margin="10" Content="Horizontal Orientation" />

</StackPanel>

Output of the above code snippet is as follows

StackPanel Control in WPF

When you have did not much more space in StackPanel when you arrange it then by implementing CanHorizontaScroll and CanVerticalScroll property you set to true to add scrolling functionality. To implement scrolling functionality you need to use ScrollViewer control.

 


Don't want to miss updates? Please click the below button!

Follow MindStick