9

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?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
MrGreggles
  • 6,113
  • 9
  • 42
  • 48

3 Answers3

32

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

Community
  • 1
  • 1
Ash
  • 5,057
  • 7
  • 35
  • 49
6
textBlock.Text = string.Format("One{0}Two", Environment.NewLine);
Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • Maybe doesn't work with SL? Anyway, I misread your question - see my updated answer. It works with WPF - not sure about SL. – Kent Boogaart Oct 13 '09 at 13:37
  • Thx for your replies. I've chosen Ash's answer, being WPF centric and as such it seems like the best way to go. – MrGreggles Oct 14 '09 at 04:25
0

When writing C# I always use "System.Environment.Newline" for the newline carriage return.

It means that you don't have to worry about character coding or what destination OS uses.

I have also found it to work on WPF GUI when called from the underlying .cs file.

ChrisBD
  • 9,104
  • 3
  • 22
  • 35