How to set the height and width of a dynamically created text block in auto?
TextBlock myTextBlock = new TextBlock() { Text = "Text Block", Width = 140, Height = 40, FontSize = 20 };
The installation width (or height) to Double.NaN equivalent to setting the width to auto in XAML.
Double.NaN
auto
TextBlock myTextBlock = new TextBlock() { Text = "Text Block", Width = Double.NaN, Height = Double.NaN, FontSize = 20 };
Also see MSDN