What the best way to get paragraphs in a WPF textblock? (newline chars?)

MrGreggles picture MrGreggles · Oct 13, 2009 · Viewed 22.6k times · Source

I have some text which has "\r\n" newline markers. I would like to have the newlines in a WPF textblock. I've tried replacing "\r\n" with "& # 13;" (without the spaces), which worked when I set the Text property in XAML, but doesn't seem to work when setting from the C# code-behind.

So...what's the standard way to convert "\r\n" to newlines in a WPF textblock?

Answer

Ash picture Ash · Oct 13, 2009

Try these for a more WPF centric solution.

TextBlock.Inlines.Add(new Run("First"));
TextBlock.Inlines.Add(new LineBreak());
TextBlock.Inlines.Add(new Run("Second"));

See also : XAML based Answer