WPF TextBlock displaying a line above multiple lines

I have a line:

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

How to associate this line with a TextBlock so that it looks like:

 Item A Item B Item C 

thanks

+4
source share
1 answer

Just make the TextBlock big enough to show three lines. TextBlock is able to wrap text if it finds a new line and carriage return in Text .

EDIT: Also make sure the newline and carriage characters are not hardcoded. I mean, there is a difference between the two:

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

and...

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

The second line will be displayed correctly in the TextBlock , but the first will be displayed in only one line as "Item A \ r \ nItem B \ r \ nItem C", because newline and carriage characters will be hardcoded instead of escape characters.

You can fix this by replacing the hard-coded characters and carriage characters with their escape sequences using:

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

or preferably:

 MyString = MyString.Replace("\\r\\n", Environment.NewLine); 
+4
source

All Articles