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

后端 未结 3 1871
北荒
北荒 2021-01-12 05:22

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 t

相关标签:
3条回答
  • 2021-01-12 05:45
    textBlock.Text = string.Format("One{0}Two", Environment.NewLine);
    
    0 讨论(0)
  • 2021-01-12 05:49

    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.

    0 讨论(0)
  • 2021-01-12 05:50

    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

    0 讨论(0)
提交回复
热议问题