tags:

views:

107

answers:

1

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

A: 

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);
Yogesh
The TextBlock is big enough but it is not processing the newline carriage return
David Ward
Can you please post the xaml. And also, what is the `TextBlock` displaying? If it is displaying "Item A\r\nItem B\r\nItem C" instead, that means the newline and carriage returns are hard coded. See my updated answer.
Yogesh
Replacing with Environment.NewLine worked a treat...thank you
David Ward