I want to set a component height to "*"
dynamically in code. How can I do that?
I know how to set it to a defined value:
DataGrid.Height = "500";
And how to set to "Auto":
DataGrid.Height = Double.NaN;
I want to set a component height to "*"
dynamically in code. How can I do that?
I know how to set it to a defined value:
DataGrid.Height = "500";
And how to set to "Auto":
DataGrid.Height = Double.NaN;
Star sizing only exists for very few components, the Grid
itself is not one of them. GridLength
has a constructor which takes the type of the sizing sheme as parameter.
If you want a component to stretch do not set any value for Width
/Height
and make sure the Horizontal
/VerticalAlignment
is Stretch
.
You can also do this using the columndefinitions/rowdefinitions with GridUnitType
, for example :
RowDefinition rd = new RowDefinition();
rd.Height = new GridLength(1.0, GridUnitType.Star);
grid.RowDefinitions.Add(rd);
See also : Grid Star-Size in code behind