开发者

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

开发者 https://www.devze.com 2022-12-08 15: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 the spaces), which worked wh

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 XAM开发者_Python百科L, 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?


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消