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

淺唱寂寞╮ 提交于 2019-12-01 01:47:18

问题


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?


回答1:


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




回答2:


textBlock.Text = string.Format("One{0}Two", Environment.NewLine);



回答3:


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.



来源:https://stackoverflow.com/questions/1559754/what-the-best-way-to-get-paragraphs-in-a-wpf-textblock-newline-chars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!