Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Set Control Height to Fill Grid Row Height

Tags:

height

wpf

grid

A WPF Grid control with 3 row and 3 columns. The Height of the Row in question is set to Auto. In the first two cells I have two controls with dynamic heights. In the third cell, I have another control I want to be automatically set to stretch within the Grid cell. I have tried VerticalAlignment="Stretch", but that simply sets Grid Row Height equal to control's height. What are my options here?

like image 927
dotNET Avatar asked Feb 18 '13 14:02

dotNET


1 Answers

Not sure what kind of control you are using in Cell 3, but most of the WPF controls will automatically stretch to fit inside the Grid cell. The row height of your grid will be set by the height of the controls in Cells 1 and 2.

If you are using some kind of custom control where the default behavior for height is different, you can set Height="Auto".

If that doesn't work either, you can do a data binding to get the actual height of the control in either cell 1 or 2. Set the Height property of your control in Cell 3 to the following:

Height="{Binding ActualHeight, ElementName=MyControlNameFromCell1, Mode=OneWay}"

EDIT

Another way that might be more robust is to do a data binding for the Height of the row. So instead of using "Auto" for the height of the row, use the data binding shown above.

like image 62
Stewbob Avatar answered Nov 01 '22 21:11

Stewbob