Grid
The Grid control is useful for arranging child controls in columns and rows. You can define absolute, proportional, and
autosized row and column geometries for the Grid.
Each child control in the Grid can be positioned in a Grid cell using column and row coordinates. These are
zero-based, and both have a zero default.
If you position multiple child controls in the same cell, they will be drawn in that cell in the sequence they appear
in the XAML. This is another strategy to implement layer stacking besides Panel.
If you omit column and row coordinates for the child controls of a Grid, they will all be drawn in the top left
corner (column=0, row=0).
It is also possible to make a child control span more than one cell in either rows or columns, or both.
Useful Properties
You will probably use these properties most often:
| Property | Description |
|---|---|
| ColumnDefinitions | Size definitions describing the widths of columns in the Grid. |
| RowDefinitions | Size definitions describing the heights of rows in the Grid. |
| ShowGridLines | Shows the gridlines between cells (as dashed lines). |
| Grid.Column | Lays out the control into the specified zero-based column. |
| Grid.Row | Lays out the control into the specified zero-based row. |
| Grid.ColumnSpan | Spans the control across 1 or more columns. |
| Grid.RowSpan | Spans the control across 1 or more rows. |
| Grid.IsSharedSizeScope | Defines the control as the containing scope for a SharedSizeGroup |
Size Definitions
You can define the size of rows and columns as:
- Absolute - sized in device-independent pixels (integer)
- Proportional - sized in proportion to remaining
Gridsize - Automatic - sized to fit the contained child control
Size definitions can be written either as a list of short codes, or fully expanded using XAML elements.
Full definitions support additional constraints such as SharedSizeGroup and specifying minimum and maximum lengths in
absolute sizes.
Absolute Size Definitions
Absolute size definitions are written as integers in the list format. For example:
ColumnDefinitions="200, 200, 300"
Using full expanded XAML, this is the same as:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="300"></ColumnDefinition>
</Grid.ColumnDefinitions>
</Grid>
Proportional Size Definitions
Proportional size definitions are written as proportions of available Grid space using an
asterisk. For example, to create two columns with the same width and then one with twice the width:
ColumnDefinitions="*, *, 2*"
Using full expanded XAML, this is the same as:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
</Grid.ColumnDefinitions>
</Grid>
Size definitions do not support percentages. One trick to overcome this is to create a definition where all proportional
values sum to 100 such as <Grid ColumnDefinitions="25*, 25*, 50*"> for 3 columns with 25%, 25%, and 50% of the remaining
available width.