Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing newline in TextBlock in windows 8

Tags:

windows-8

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?

like image 741
Milan Aggarwal Avatar asked Oct 06 '12 07:10

Milan Aggarwal


People also ask

How to add newline or line break in textblock in XAML?

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 .

How to display backslash in text block 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.

Why can't I add objects to a textblock?

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.


1 Answers

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.

like image 171
Inder Kumar Rathore Avatar answered Oct 13 '22 20:10

Inder Kumar Rathore