开发者

WPF TextBlock Displaying String Over Multiple Lines

I have a string:

Item A\r\nItem B\r\nItem C

How 开发者_运维技巧can I bind this string to a TextBlock so that it appears as:

Item A
Item B
Item C

Thanks


Just make the TextBlock big enough to show three lines. TextBlock is capable of wrapping the text if it finds newline and carriage return in Text.

EDIT: Also, make sure that the newline and carriage returns are not hard coded. What I mean is that there is a difference between these two:

MyString = @"Item A\r\nItem B\r\nItem C";

and...

MyString = "Item A\r\nItem B\r\nItem C";

The second string will display correctly in the TextBlock but the first will just get displayed in a single line as "Item A\r\nItem B\r\nItem C" because the newline and carriage characters are hard coded instead of being escape characters.

You can fix that by replacing the hard coded newline and carriage characters with their escape sequences, by:

MyString = MyString.Replace("\\r\\n", "\r\n");

or preferably by:

MyString = MyString.Replace("\\r\\n", Environment.NewLine);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜