How do I set a grid column/row size without defining each line?

wpf, xaml

Solution

What you describe is called `UniformGrid`. It has `Columns` and `Rows` properties by which you can set the number of rows or columns that you want.

If you don't set these properties, the `UniformGrid` will try to layout the children as close to a square as it can. In this situation, it prefers to increase the number of columns before increasing the number of rows.

It's an obscure panel, but it's extremely powerful when used correctly.

Problem

I have a grid. I have to define each column and row manually, like this: ``` <Window x:Class="GridBuild" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="GridBuild" Height="300" Width="300"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> </Grid> ``` I want to define the number of rows and columns with a single line, something like this: ``` <Window x:Class="GridBuild" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="GridBuild" Height="300" Width="300"> <Grid> <Grid.NumberOfRows="2"/> <Grid.NumberOfColumns/> </Grid> </Window> ```

Original source