Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent Line Break or new line in silverlight textBox

Tags:

I am having hard time to match Special characters set in Silverlight. I only on the following:

To represent a LineBreak in Silverlight TextBlock:

use : > lineBreak <

But What do I use to represent a New Line or LineBreak In Silverlight TextBox??

Example : I want this one line mag : This is line one. This is line two

into this :

This is line one. This is line two.

it seems this \r\n is not working. This is line one \r\n

like image 231
MilkBottle Avatar asked Jan 25 '11 07:01

MilkBottle


People also ask

How do you insert a line break character?

Press ALT+ENTER to insert the line break.

What is the symbol for line break?

The glyph for the control character for a hard return is usually a pilcrow (¶), and for the manual line break is usually a carriage return arrow (↵).

How do you put a line break inside a string?

Adding Newline Characters in a String In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

How do you insert a line break in XAML?

XAML attributes of type String may contain any special characters, as long as those are referenced as hex-codes. For example, a simple line break ( \n ) would be represented as &#x0a; , for \r\n you'd use &#x0d;&#x0a; and so on.


2 Answers

The bottom line at the top

<TextBox Text="This is line one!&#13;This is line two!" /> 

Linebreak Weirdness in the Silverlight TextBox

If you are going to be initialising content of a TextBox with literal text in Xaml in a similiar way that you might a TextBlock then you need a reliable way to represent the line break character the Silverlight uses in Xaml.

Silveright uses a CR character (0x0D - ASCII 13) to represent a linebreak which in C# you include in a string literal as \r. However Xaml isn't C# so you can't use \r in Xaml.

Xaml is fundementally XML but with some Xaml parsing oddities. Just including a linebreak, as Derek has in his answer, directly in the Xaml will not work at runtime (although the designer displays it as expected). You might think that this because Xml uses the LF character (0x0A) as its linebreak character. However in code you can assign a string containing "\r" or "\n" to the Text property and the TextBox will show a new line. In fact you can assign the sequence "\r\n" and it will show a single new line (not two new lines).

Ultimately you can use the Xml character code entity to represent a \r in Xaml "&#13;" which survives the Xaml parsing process for reason which I cannot actually explain.

like image 181
AnthonyWJones Avatar answered Sep 18 '22 19:09

AnthonyWJones


In XAML you can simply use the LineBreak:

<TextBlock Name="textBlock1" >line 1 <LineBreak /> line 2</TextBlock> 
like image 21
Ahmet Avatar answered Sep 20 '22 19:09

Ahmet