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:
In the above example we have two keywords: * and Auto. Here is explanation for them:
The "Auto" keyword is used to have the row or column geometry be determined by the containing control's definitions.
The * 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 Column 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.
Here is another example showing the difference between those two.
First let's create sample 2x2 grid in our View, we can achieve this simply by writing code looking like this:
As you can see we created equal grid, I left ShowGridLines
parameter set to True
for better visibility.
Now let's fill our grid with some elements, I will fill every field with button, you can use anything you want.
Now our View code look's like this:
In this moment our asterisk symbols are forcing our grid to become equal, now let's see what will happen when we replace asterisk with the Auto keyword
As you can see our grid become sticky to its content, it is very useful when we have components with variable Height
property.
This new View code look's like this:
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.
Common Properties
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
最后更新于