When declaring a Grids columns and rows in XAML you simply specify the rows you want and the columns you want under the Grid.RowDefinition and Grid.ColumnDefinition sections.
For example:
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
</Grid>
You can set a static width and height with any integer value. Alternatively you can use “*” which is used to indicate the row or column will take up any space not already used by the other columns or row. The other option is to set the row or column to “Auto”. This will cause the row or column to match the width or height of the controls that are in it.
To dynamically add rows and columns to a Grid control you will need to create a ColumnDefinition/RowDefinition object for each column/row you want, set the width/height and add it to the ColumnDefinitions/RowDefinitions collection of the Grid control.
For example:
LayoutRoot.ColumnDefinitions.Clear();
LayoutRoot.RowDefinitions.Clear();
for (column = 0; column < columnCount; column++)
{
ColumnDefinition cd = new ColumnDefinition();
cd.Width = new GridLength(columnWidth);
LayoutRoot.ColumnDefinitions.Add(cd);
}
for (row = 0; row < rowCount; row++)
{
RowDefinition rd = new RowDefinition();
rd.Height = new GridLength(rowHeight);
LayoutRoot.RowDefinitions.Add(rd);
}
When you specify what Grid Row and Column you want a control to belong to through XAML you do this by simply specifying the Grid.Row and Grid.Column properties in the declaration of the control.
For example, say you have a control called MapTile. To place it in Row 1 and Column 2 (zero based index) you would declare it in your XAML as such:
<my:MapTile Grid.Row="1" Grid.Column="2" />
To do this dynamically however you have to call Grid.SetRow() and Grid.SetColumn().
For example:
MapTile mt = new MapTile();
Grid.SetColumn(mt, column);
Grid.SetRow(mt, row);
Thanks,
–Mike







Hi Michael,
Thanks so much for your blog. It is so helpful.
My question is more related to Tip#19 in your old blog on silverlight.net that talks about Isolated storage.
I’m trying to capture user preference from a checkbox that says “Do not show this again”. How do I do that with Isolated Storage. Or should I use cookies.
Please let me know.
Thanks!
Solomon
Mike,
Porting this to the MVVM pattern how would you bind the RowDefintions to a view model?
Thanks.