开发者

WPF TextBlock Displaying String Over Multiple Lines

开发者 https://www.devze.com 2023-01-17 10:24 出处:网络
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:

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

精彩评论

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