Mar 28

Silverlight and WPF support a flexible layout management system that enables developers and designers to easily coordinate how controls are positioned within a UI surface. This layout system supports both a fixed position model where controls are positioned using explicit coordinates, as well as a more dynamic position model where layout and controls can be automatically sized and flowed as the browser resizes.

Developers using Silverlight and WPF use layout panels to coordinate the position and resizing of controls contained within them. The built-in layout panels in Silverlight 2 include three of the most commonly used ones in WPF:

  • Canvas
  • Stack Panel
  • Grid

Canvas Panel

The Canvas panel is a pretty basic layout panel that supports positioning controls contained within it using explicit coordinates.

You position elements in a Canvas using a XAML feature called “Attached Properties” – which allow you to specify a control’s position relative to its immediate parent Canvas control’s Left, Top, Right or Bottom coordinates. Attached properties are useful as they allow a parent panel to extend the property set of a control contained within it. Canvas, by defining an attached property for “Top” and ”Left” basically adds the ability to define left and top attachment on Button (or any other UI element that is added to the Canvas), without any need to actually add a property to the Button class, or modify the Button class in any way.

We could add two buttons to a Canvas container, and position them both 50 pixels from the left of the Canvas, and 50 and 150 pixels from the top using XAML like below (the Canvas.Left and Canvas.Top attributes are examples of the attached property syntax):

Alomohora - Silverlight - Layout Management1

The code

<UserControl x:Class="SilverlightSample.Page"

   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   Width="300" Height="300">

 

    <Canvas Background="#FF5C7590">

        <Button Content="Button1" Width="100" Height="50" Canvas.Left="50" Canvas.Top="50"></Button>

 

        <Button Content="Button2" Width="100" Height="50" Canvas.Left="50" Canvas.Top="150"></Button>

    </Canvas>

</UserControl>

This would then render our buttons like below:

Install Microsoft Silverlight

Download source code for Canvas Panel

While the Canvas is useful for scenarios where the UI elements contained within it never move, it tends not to be very flexible as you add more controls into it or handle scenarios where the UI needs to resize or move. In cases like these you end up having to manually write code yourself to move things around inside the Canvas (which is a pain). A better solution for these dynamic scenarios is typically to use an alternative layout panel that has built-in semantics to-do this for you – like the StackPanel and Grid.

StackPanel

The StackPanel control is a simple layout panel that supports positioning its contained controls in either a row or column layout. StackPanels are typically used in scenarios where you want to arrange a small subsection of the UI on your page.

For example, we could use the StackPanel to vertically arrange three buttons on our page using XAML markup like below:

Alomohora - Silverlight - Layout Management3

The code

<UserControl x:Class="SilverlightSample.Page"

   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   Width="400" Height="300">

 

    <StackPanel Background="#FF5C7590">

        <Button Content="Button1" Width="100" Height="50" Margin="10"></Button>

 

        <Button Content="Button2" Width="100" Height="50" Margin="10"></Button>

 

        <Button Content="Button3" Width="100" Height="50" Margin="10"></Button>

    </StackPanel>

</UserControl>

At runtime the StackPanel would then automatically arrange the Button controls in a vertical stack for us like below:

Install Microsoft Silverlight

Download source code for Stack Panel

We could alternatively set the “Orientation” property of the StackPanel to be “Horizontal” instead of Vertical (which is the default):

Alomohora - Silverlight - Layout Management5

<UserControl x:Class="SilverlightSample.Page"

   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   Width="400" Height="300">

 

    <StackPanel Orientation="Horizontal" Background="#FF5C7590">

        <Button Content="Button1" Width="100" Height="50" Margin="10"></Button>

 

        <Button Content="Button2" Width="100" Height="50" Margin="10"></Button>

 

        <Button Content="Button3" Width="100" Height="50" Margin="10"></Button>

    </StackPanel>

</UserControl>

This will then cause the StackPanel to automatically arrange the Button controls in a horizontal row like below:

Download source code for Stack Panel (Orientation)

Grid Panel

The Grid control is the most flexible layout panel, and supports arranging controls in multi-row and multi-column layouts. It is conceptually similar to an HTML Table element.

Unlike an HTML Table, you don’t embed controls within column and row elements. Instead you specify a Grid’s Row and Column definitions using <Grid.RowDefinitions> and <Grid.ColumnDefinitions> properties that are declared immediately under the <Grid> control. You can then use the XAML “Attached Property” syntax on controls contained within the grid to indicate which Grid row and column they should be populated within.

For example, we could declare a Grid layout has three rows and three columns, and then position 4 buttons within it using XAML like below:

Alomohora - Silverlight - Layout Management7

The code

<UserControl x:Class="SilverlightSample.Page"

   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   Width="400" Height="300">

 

    <Grid Background="#FF5C7590">

        <Grid.RowDefinitions>

            <RowDefinition Height="60" />

            <RowDefinition Height="60" />

            <RowDefinition Height="60" />

        </Grid.RowDefinitions>

 

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="110"/>

            <ColumnDefinition Width="110"/>

            <ColumnDefinition Width="110"/>

        </Grid.ColumnDefinitions>

 

        <Button Content="Button1" Width="100" Height="50" Grid.Column="1" Grid.Row="0"></Button>

        <Button Content="Button1" Width="100" Height="50" Grid.Column="2" Grid.Row="1"></Button>

        <Button Content="Button1" Width="100" Height="50" Grid.Column="0" Grid.Row="1"></Button>

        <Button Content="Button1" Width="100" Height="50" Grid.Column="1" Grid.Row="2"></Button>

    </Grid>

</UserControl>

The Grid layout panel would then position the Button elements for us like so:

Install Microsoft Silverlight

Download source code for Grid Panel

n addition to supporting absolute sizing (for example: Height=”60″) the Grid’s RowDefinition and ColumnDefinition controls also support an AutoSizing mode (Height=”Auto”), which automatically sizes the Grid or Row based on the size of the content contained within it (you can also optionally specify maximum and minimum size constraints – which can be very useful).

The Grid’s Row and ColumnDefinitions also support a feature called “Proportional Sizing” – which enables the size of a Grid’s Rows and Columns to be spaced proportionally relative to each other (for example: you could have the second row grow at 2x the rate of the first one).

You’ll find that the Grid provides a ton of power and flexibility – and it will probably be the most common layout panel control you’ll end up using.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Technorati
  • Twitter

Related Posts:

  1. Missing Microsoft SQL Server Management Studio
  2. Multi Page Silverlight Application
  3. WPF (Layout Controls)
  4. Tooltip for Silverlight
  5. Silverlight (Creating Hello World)

One Response to “Silverlight (Layout Management)”

  1. Tristan Cipcic Says:

    Very sensible question, comment and an answer from which, I have learned something today.

Leave a Reply