WPF Auto height in code WPF Auto height in code wpf wpf

WPF Auto height in code


Perhaps this link will help you.

At times, you may want toprogrammatically set the Height orWidth of a WPF element to Auto incode. To do this, just use theDouble.NaN (Not a Number) value.

For example, in C#:

this.txtName.Width = Double.NaN;


You can use

RowDefinition rd = new RowDefinition();  rd.Height = GridLength.Auto;  ContentGrid.RowDefinitions.Add(rd);


While the question has been answered with the code-behind (as asked), here is the same solution in XAML for basic control properties who don't have "auto":

xmlns:sys="clr-namespace:System;assembly=mscorlib"Height="{x:Static sys:Double.NaN}"

Posting because google took me here when searching for XAML solution for a TextBox (auto doesn't exist).