In this article let us figure out how to utilize Stack panel Layout in a Silverlight application. StackPanel is an alternate most critical board in Silverlight. It is predominantly helpful when we need to demonstrate some of your Silverlight components either Horizontally or Vertically.

Of course, open visual studio and select Silverlight project. We can perceive that there is a Grid format in our MainPage.xaml. Delete the default Grid layout and just drag and drop the Stack panel Layout into our application. The code for this looks like as:
<StackPanel Orientation="Vertical" Background="White" Height="200" Width="100">
            <Rectangle Height="50" Width="100" Fill="Red" />
            <Rectangle Height="50" Width="100" Fill="Blue" />
           <Rectangle Height="50" Width="100" Fill="Gray" />
            <Rectangle Height="50" Width="100" Fill="Goldenrod" />      
</StackPanel>

From the above code we can perceive that I put 4 rectangles in our Stack panel layout. What's more I have given a worth "Vertical" to the property "Orientation" of Stack board design. It implies that all the four rectangles will allign in vertical request. Presently give us a chance to assume we need all the rectangles to be adjusted in level request, then we ought to give the worth "Flat" to the property "Introduction" of Stack board format.

The code for this looks like as:
Presently give us a chance to assume we need all the rectangles to be adjusted in flat request, then we ought to give value “Horizontal” to the property “Orientation”  of Stack panel layout. The code for this looks like as
<StackPanel Orientation="Horizontal" Background="White" Height="100" Width="300">
            <Rectangle Height="100" Width="60" Fill="Red" />
            <Rectangle Height="100" Width="60" Fill="Blue" />
            <Rectangle Height="100" Width="60" Fill="gray" />
            <Rectangle Height="100" Width="60" Fill="Goldenrod" />      
</StackPanel>

And this is the output:

Here is the sample code for the above explanation:

MainPage.xaml
<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
    xmlns:d=http://schemas.microsoft.com/expression/blend/2008
    xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <StackPanel Orientation="Vertical" Background="White" Height="200" Width="100">          

            <Rectangle Height="50" Width="100" Fill="Red" />
            <Rectangle Height="50" Width="100" Fill="Blue" />
            <Rectangle Height="50" Width="100" Fill="Gray" />
            <Rectangle Height="50" Width="100" Fill="Goldenrod" />     
    </StackPanel>
</UserControl>

That above code will give you rectangles with vertical alignment. If you want horizontal alignment then replace complete code between <StackPanel> and </StackPanel> with the horizontal code.

HostForLIFE.eu Silverlight 5 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.