C# – How to put a new line into a wpf TextBlock control

cnewlinetextblockwpf

I'm fetching text from an XML file, and I'd like to insert some new lines that are interpreted by the textblock render as new lines.

I've tried:

<data>Foo bar baz \n baz bar</data>

But the data is still displayed without the new line. I set the contents of <data> via the .Text property via C#.

What do I need to put in the XML in order for it to render the new line in the GUI?

I've tried something like this manually setting the text in the XAML:

<TextBlock Margin="0 15 0 0" Width="600">
There &#10;
is a new line.
</TextBlock>

And while the encoded character doesn't appear in the GUI it also doesn't give me a new line.

Best Answer

You can try putting a new line in the data:

<data>Foo bar baz 
 baz bar</data>

If that does not work you might need to parse the string manually.

If you need direct XAML that's easy by the way:

<TextBlock>
    Lorem <LineBreak/>
    Ipsum
</TextBlock>