0
votes

I am aware that I can use Grid.Rowdefinitions to define the number of rows and their properties on a WPF Grid control.

However is there a way to set the grid to automatically grow/add rows as controls are added, without having to explicitly state it?

2
"is there a way to set the grid to automatically grow/add rows as controls are added" - No. You will have to write code which adds RowDefeinitions somewhere - ASh
Try different panel: dockpanel or stackpanel - El Barrent
What are you trying to do? Have you tried using ListBox or a ListView instead? - XAMlMAX
@XAMlMAX, I keep adding checkboxes to my grid and it's a hassle to every time add the checkbox, add a new row, set the new checkbox's Grid.Row property and that same property of the other checkboxes, etc. - J. Doe

2 Answers

1
votes

However is there a way to set the grid to automatically grow/add rows as controls are added, without having to explicitly state it?

No, there isn't. Depending on your requirements, you probably want to replace the Grid with another Panel like for example a StackPanel or a UniformGrid with a single column:

<UniformGrid x:Name="grid" Columns="1" />

Then you don't need to care about setting any Grid.Row property.

0
votes

You can do that in your code behind. Define following in the .xaml of your window:

 <Grid x:Name="YourGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>  
    </Grid.RowDefinitions>
  </Grid> 

Now use a loop to create as many rows as you need:

    foreach(Control control in controlls)
    {
        YourGrid.RowDefinitions.Add(new RowDefinition());

        YourGrid.Children.Add(control);
        Grid.SetRow(control , YourGrid.RowDefinitions.Count - 1);
    }

If you have a lot of controls this could help you. It will add the control automatically into the created row. If you don't want to add the controls and rows in the code behind, you will have to add the rows manually. As far as I know there is no way to automate it.