In today’s lesson we will be learning about a Layout control named the StackPanel control. What the StackPanel control does is organize a number of controls in a row or column. You also have the ability to change the Horizontal and Vertical Alignment as well so positioning can be set to your liking.

The only con for this type of layout control is that you can only have one control per column or row, depending on the Orientation you set the StackPanel control to.

To begin, let’s create a new project for today’s example. Once we have done so we can add a StackPanel control to our XAML file from the ToolBox.

Yes, it is possible to find a good web host. Sometimes it takes a while to find one you are comfortable with. After trying several, we went with Server Intellect and have been very happy thus far. They are by far the most professional, customer service friendly and technically knowledgeable host we’ve found so far.

Code Block
MainPage.xaml
StackPanel Control
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

</StackPanel>
Now we can add three Button controls to demonstrate the how the controls are organized.
Code Block
MainPage.xaml
Organize Button Controls in StackPanel Control
<StackPanel Height="217" HorizontalAlignment="Left" Margin="12,0,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="456" Grid.Row="1">
    <Button Content="Button" Height="71" Name="button1" Width="160" />
    <Button Content="Button" Height="71" Name="button2" Width="160" />
    <Button Content="Button" Height="71" Name="button3" Width="160" />
</StackPanel>
We can now go ahead and edit the properties of the Buttons for Horizontal Alignment within the StackPanel. What this does is position the Buttons similar to how you would do in a web application using CSS.
Code Block
MainPage.xaml
Horizontal Alignment
<StackPanel Height="194" HorizontalAlignment="Left" Margin="12,223,0,0" Name="stackPanel2" VerticalAlignment="Top" Width="456" Grid.Row="1">
    <Button Content="Button" Height="71" Name="button4" Width="160" HorizontalAlignment="Left" />
    <Button Content="Button" Height="71" Name="button5" Width="160" />
    <Button Content="Button" Height="71" Name="button6" Width="160" HorizontalAlignment="Right" />
</StackPanel>
We also have the option to change the Orientation of the StackPanel Control to Horizontal, instead of the default setting of Vertical, like so:

We stand behind Server Intellect and their support team. They offer dedicated servers, and they are now offering cloud hosting

Code Block
MainPage.xaml
Horizontal Orientation
<StackPanel Height="172" HorizontalAlignment="Left" Margin="12,423,0,0" Name="stackPanel3" VerticalAlignment="Top" Width="456" Grid.Row="1" Orientation="Horizontal">
    <Button Content="Button" Height="71" Name="button7" Width="160" />
    <Button Content="Button" Height="71" Name="button8" Width="160" />
    <Button Content="Button" Height="71" Name="button9" Width="160" />
</StackPanel>
Another benefit of the StackPanel is that it can be set up similar to Panel controls in web development to contain small groups of controls that can be moved together as a whole, as multiple StackPanels can be placed on the same display at one time.

And there you have it; we have just covered a few of the different ways to use the StackPanel control in Silverlight for the Windows Phone 7 platform using Microsoft Visual Studio 2010. If you have any other questions or concerns and have not yet downloaded the source code for this project, I strongly suggest doing so. Seeing it in its entirety can help clear up many of your questions. Thank you for your time and I hope this has been helpful, take care.

We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud hosting!

WP7StackPanelTut.zip