Sabuncu
Sabuncu

Reputation: 5264

TextBlock Text property still null after adding Inlines

In a very simple WPF application, I have the following code in the button1_click() method:

textBlock1.Inlines.Add(new Run("TextBlock is designed to be lightweight."));
canvas.Children.Add(textBlock1);
Debug.Print("textBlock1.Text: {0}", textBlock1.Text);

The Debug.Print statement prints nothing for the Text property, but the actual text (i.e. "TextBlock is designed...") is visible in the TextBlock control on the canvas. Why doesn't the Text property depict an exact copy of the Inlines?

PS: There is no data binding, etc. used. The project is very simple, with a TextBlock and a Button on a Canvas inside MainWindow, and minimal XAML; everything is handled in the code-behind.

Upvotes: 3

Views: 391

Answers (1)

123 456 789 0
123 456 789 0

Reputation: 10865

You can just directly set the Text property of the Textblock. If you have a text that needs special formatting like bold, italic, underline, etc in for each words then that's the time you want to use the Inlines.

If you want to debug the actual Text you want to extract the Run into a separate variable and access its Text property.

var run = new Run("TextBlock is designed to be lightweight");
Debug.Print(run.Text);

Upvotes: 3

Related Questions