Grid
The Grid
control is a Panel
control useful for organizing other controls in columns and rows. ColumnDefinition
and RowDefinition
properties are used to define absolute, relative, or proportional row and column geometries for the grid.
Each control in the grid will be placed using the Grid.Column
and Grid.Row
additional properties. It is also possible to have controls that span multiple rows and/or columns by using the ColumnSpan
and RowSpan
properties.
Reference
Source code
Examples
Grid Using Properties and Spanning Columns
Below is an example that shows:
- Configuring the grid using the ColumnDefinition and GridDefinition properties directly
- How to assign the cell for a given component
- Showing effects of spanning rows/columns An example of a Grid with 3 equal Rows and 3 Columns with (1 fixed width), (2 grabbing the rest proportionally) would be:
<Grid ColumnDefinitions="100,1.5*,4*" RowDefinitions="Auto,Auto,Auto" Margin="4">
<TextBlock Text="Col0Row0:" Grid.Row="0" Grid.Column="0"/>
<TextBlock Text="Col0Row1:" Grid.Row="1" Grid.Column="0"/>
<TextBlock Text="Col0Row2:" Grid.Row="2" Grid.Column="0"/>
<CheckBox Content="Col2Row0" Grid.Row="0" Grid.Column="2"/>
<Button Content="SpansCol1-2Row1-2" Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" Grid.ColumnSpan="2"/>
</Grid>
In the above example the "Auto" keyword is used to have the row or column geometry be determined by the containing control's definitions. The asterisk is used for denoting proportional spacing. The multiplier used in front of the proportional spacing value is used to figure out the relative size for the proportional columns. All proportional columns fit in the space left behind after all explicit values and "Auto" values are calculated. So for the above example the Column 1 will get 1.5 parts plus Column 2 will get 4 parts of the remainder of the space that Colum 0 left. Lastly, the Button itself will fill in from the initial Cell 1,1 over one column and down one row because Grid.RowSpan
and Grid.ColumnSpan
are set to occupy two units instead of one.
Using Verbose Row/Column Definitions
For more complex row and column definitions it's possible to explicitly use Grid.ColumnDefinitions
and Grid.RowDefinitions
XAML fields to provide access to these additional settings. The below code produces is exactly the same except for the fact we set the minimum width on the second column to be 300.
<Grid Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="1.5*" MinWidth="300"/>
<ColumnDefinition Width="4*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="Col0Row0:" Grid.Row="0" Grid.Column="0"/>
<TextBlock Text="Col0Row1:" Grid.Row="1" Grid.Column="0"/>
<TextBlock Text="Col0Row2:" Grid.Row="2" Grid.Column="0"/>
<CheckBox Content="Col2Row0" Grid.Row="0" Grid.Column="2"/>
<Button Content="SpansCol1-2Row1-2" Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" Grid.ColumnSpan="2"/>
</Grid>
Common Properties
Property | Description |
---|---|
ColumnDefinitions |
A collection of ColumnDefinition s describing the width and max or min width of a column |
RowDefinitions |
A collection of RowDefinition s describing the height and max or min height of a row |
Grid.Column |
Attached property to assign a control into a zero based column |
Grid.Row |
Attached property to assign a control into a zero based row |
Grid.ColumnSpan |
Attached property to define the number of columns a control will span |
Grid.RowSpan |
Attached property to define the number of rows a control will span |