Silverlight for Windows Phone provides a flexible layout system that enables you to specify how controls are positioned on the screen. This QuickStart describes how to design a user interface that automatically sizes to various screen resolutions.
This QuickStart contains the following sections:
Layout is the process of sizing and positioning objects in your Silverlight application for Windows Phone. To position visual objects, you must put them in a Panel control or other container object. Silverlight provides various Panel controls, such as Canvas, StackPanel, and Grid, that serve as containers and enable you to position and arrange the controls.
The Silverlight for Windows Phone layout system supports both an absolute layout and a dynamic layout. In an absolute layout, controls are positioned using explicit x/y coordinates (for example, by using a Canvas). In a dynamic layout, the user interface automatically sizes to various screen resolutions (for example, by using a StackPanel or a Grid).
In an absolute layout, you arrange child elements in a layout panel by specifying their exact locations relative to their parent element. Absolute positioning doesn't consider the size of the screen. If the application requires absolute positioning of UI elements, you can design different pages for different screen resolutions or use scaling as an alternative.
Silverlight for Windows Phone provides a Canvas control to support absolute positioning. By default, when you create a new Windows Phone application project, the root layout panel is a Grid. To create a layout based on absolute positioning, you must replace the Grid with a Canvas.
To position controls on a Canvas, you set the following attached properties on the control. When you use the designer in Visual Studio, these properties are updated when you drag controls onto the design surface.
In a dynamic layout, the user interface appears correctly on various screen resolutions. You arrange child elements by specifying how they should be arranged and how they should wrap relative to their parent. For example, you can arrange controls on a panel and specify that they should wrap horizontally. To use automatic or proportional sizing, you must assign special values to the Height and Width properties. The following are the recommended settings for a dynamic layout:
Auto sizing is used to allow controls to fit their content, even if the content changes size. Star sizing is used to distribute available space among the rows and columns of a grid by weighted proportions. In XAML, star values are expressed as * (or n*). For example, if you have a grid with four columns, you could set the widths of the columns to the values shown in the following table.
| Column_1 | Auto | The column will size to fit its content. |
| Column_2 | * | After the Auto columns are calculated, the column gets part of the remaining width. Column 2 will be one-half as wide as Column 4. |
| Column_3 | Auto | The column will size to fit its content. |
| Column_4 | 2* | After the Auto columns are calculated, the column gets part of the remaining width. Column 4 will be two times as wide as Column 2. |
For more information about Auto and star sizing, see the GridUnitType enumeration.
The built-in layout panels in Silverlight for Windows Phone are are Canvas, StackPanel, and Grid.
Canvas defines an area within which you can explicitly position child elements by coordinates relative to the Canvas area. Canvas is useful for scenarios where the UI elements contained within it never move.
You can position the control inside the Canvas by specifying x and y coordinates. The x and y coordinates are specified by using the Canvas.Left and Canvas.Top attached properties. Canvas.Left specifies the object's distance from the left side of the containing Canvas (the x-coordinate), and Canvas.Top specifies the object's distance from the top of the containing Canvas (the y-coordinate).
The following XAML shows how to position a rectangle 30 pixels from the left and 200 pixels from the top of a Canvas.
The preceding XAML produces output that is similar to the following illustration.
The StackPanel is a simple layout panel that arranges its child elements into a single line that can be oriented horizontally or vertically. You can use the Orientation property to specify the direction of the child elements. The default value for the Orientation property is Vertical. StackPanels are typically used in scenarios where you want to arrange a small subsection of the UI on your page.
The following XAML shows how to create a vertical StackPanel of items.
The preceding XAML produces output that is similar to the following illustration.
The Grid control is the most flexible layout panel, and supports arranging controls in multi-row and multi-column layouts. You can specify a Grid's Row and Column definitions by using Grid.RowDefinitions and Grid.ColumnDefinitions properties that are declared immediately within the Grid control. You can position objects in specific cells of the Grid by using the Grid.Column and Grid.Row attached properties. You can distribute space within a column or a row by using Auto or star sizing. Auto and star sizing were is explained earlier in this QuickStart. Content can span across multiple rows and columns by using the RowSpan and ColumnSpan attached properties.
Child elements of a Grid are drawn in the order in which they appear in markup or code. As a result, layered order (also known as z-order) can be achieved when elements share the same coordinates. For more information about z-order, see ZIndex.
The following XAML shows how to create a Grid with three rows and two columns. The height of the first and third rows is just large enough to contain the text. The height of the second row fills up the rest of the available height. The width of the columns is split equally within the available container width.
The preceding XAML produces output that is similar to the following illustration.
If you're creating a complex layout that isn't easily achieved by using Canvas, StackPanel, or Grid, you can create a custom panel, which allows you to define layout behavior for the panel's children. To do this, derive from Panel and override its MeasureOverride and ArrangeOverride methods. For more information, see Custom Panels.
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/