How to dynamically add RowDefinition or ColumnDefinition to a Grid with binding?
You can use attached properties for a Grid
that modify the RowDefinitions
and ColumnDefinitions
when those properties are set or changed.
It will allow you to write your Grid
like this:
<Grid local:GridHelpers.RowCount="{Binding MaxGridRow}"
local:GridHelpers.ColumnCount="3" />
Then just expose a property from your ViewModel
which returns the largest row number in the Cells
collection.
You can find a detailed implementation of those properties on my blog.
If you had access to the grid from code-behind you could do this:
var rowDefinition = new RowDefinition();
rowDefinition.Height = GridLength.Auto;
grid.RowDefinitions.Add(rowDefinition);