3

How to set height and widhth of dynamically created text block to auto?

TextBlock myTextBlock = new TextBlock() { Text = "Text Block", Width = 140, Height = 40, FontSize = 20 };
Romasz
  • 29,662
  • 13
  • 79
  • 154
Mihir
  • 89
  • 2
  • 13

1 Answers1

7

Setting width (or height) to Double.NaN is the equivalent of setting the width to auto in XAML.

TextBlock myTextBlock = new TextBlock() { Text = "Text Block", Width = Double.NaN, Height = Double.NaN, FontSize = 20 };

Also see MSDN

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150