Grid is one of the Panel elements that enable complex layout. By default, a Grid contains one row and one column. To define multiple rows and columns use the ColumnDefinitions and RowDefinitions collections. Each RowDefinition and ColumnDefinition object inside the RowDefinitions and ColumnDefinitions collections define a single row or column. The RowDefinition and ColumnDefinition objects also define the size of each row and column using the GridLength object. For a definition of each unit type, see GridUnitType.
Note: |
|---|
Grid is designed to use for layout of your application's user interface. It is not intended for data display. For displaying and binding to sets of data, use DataGrid. |
You can position objects in specific cells of the Grid by using the Grid..::.Column and Grid..::.Row attached properties.
Columns and rows can take advantage of Star sizing to distribute remaining space proportionally. When Star is selected as the height or width of a row or column, that column or row receives a weighted proportion of the remaining available space. Star sizing is the default behavior.
The following illustration shows a grid where the height of the first two rows is set to * and the last row is 2*.
.png)
Columns and rows can use Auto sizing to distribute space evenly based on the size of the content that is within a column or row. The following illustration shows 3 columns using Auto sizing.
.png)
Content can span across multiple rows and columns using the RowSpan and ColumnSpan attached properties.
Setting the Margin property on the child elements in a grid sets the distance between the element and its grid cell boundaries. The HorizontalAlignment and VerticalAlignment properties describe how the child element should be positioned within the grid cell. You can precisely position child elements of a Grid by using a combination of the Margin property and alignment 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 table summarizes the other available layout containers provided by Silverlight.
Panel name | Description |
|---|
Canvas
| Defines an area where you can explicitly position child elements by X and Y values. |
StackPanel
| Arranges child elements into a single line that can be oriented horizontally or vertically. |
For scenarios that require application layout that is not possible using any of the predefined Panel elements, custom layout behaviors can be achieved by inheriting from Panel and overriding the default measure and arrange behavior by using the MeasureOverride and ArrangeOverride methods. For more information, see Custom Panels.