I want to assign the TextBlock's Text
in my code behind and display it on the screen. It might contain new line character
also. But somehow the TextBlock
is not printing that character. I have used the following combinations in my text to print the new line character




\n
\r\n
Has anyone done this? can you help me?
How to Add Newline or line break in the Text attribute of TextBlock in Xaml ? @isenthil"> </TextBlock> @isenthil"> The other option to achieve the same is to use the xml:space attribute of the textblock as shown below. We do have one more option to get the line break in Xaml .
:) Putting " " (backslash n) in the TextBlock's Text attribute in XAML simply displays " " in the text. To use " ", you have to specify it programatically as others have pointed out. Thanks.
Maybe this string is actually a localised resource which will be used throughout an application and you can't just add objects to the textblock. You can define a string with carriage return and line break in XAML as a resource, there are a couple of tricks to this though. Here's an example Resource Dictionary.
In XAML
you can do like this
<TextBlock>Hello how are you?<LineBreak/>I'm fine</TextBlock>
In code you can do like this
textBlock.Text = "Hello how are you?\nI'm fine.";
Both are working for me.
Edited
For your scenario you can do this
string str = @"Hello how are you?\nI'm fine.";//This is your actual string containing \n as character
or in your case
string str = _arr[index];
str = str.Replace(@"\n", "\n");
Replace "\n" string with new line character.
P.S. It will create problem where you actually want to show \n
string instead of new line character.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With