How to represent Line Break or new line in silverlight textBox
Asked Answered
W

7

21

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

Witkowski answered 25/1, 2011 at 7:47 Comment(0)
E
48

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.

Erastian answered 25/1, 2011 at 14:28 Comment(2)
+1 for although the designer displays it as expected. Made me realize that Designer may not be displaying 'newline' with &#13; but at runtime textblock would ;)Haemo
Works well in WP8.1 too!Idio
C
18

In XAML you can simply use the LineBreak:

<TextBlock Name="textBlock1" >line 1 <LineBreak /> line 2</TextBlock>
Crumble answered 25/1, 2011 at 10:5 Comment(4)
poster is asking about a TextBox, not a TextBlock.Peafowl
The title is about a "TextBox", but if you look in his post : "... To represent a LineBreak in Silverlight TextBlock: ..."Urba
@danbord: I think you need to read the question more carefully.Erastian
It's true that this is not for TextBox, like the author requested, but this was very helpful for me solving my TextBlock line break need. :-)Myo
M
10

To add a line break to the Text property of a TextBox in XAML, use the ASCII character code for a linefeed as shown in the following example:

<TextBox x:Name="_test" Height="150" Text="This is line one.
This is line two." />

To add a line break to the Text property of a TextBox in code-behind, use the Environment.NewLine static value (which is the same as \r\n) as shown in the following code example:

this._test.Text = string.Format(
    "This is line one.{0}This is line two.",
    Environment.NewLine);
Maddy answered 25/1, 2011 at 9:56 Comment(0)
H
2

If you want to display a string with a carriage return in it, just use a string with a carriage return in it:

MyTextBlock.Text = @"line 1
line2";
Hilar answered 25/1, 2011 at 9:53 Comment(1)
The problem with this approach is that it includes the sequence \r\n to represent a new line. This is inconsistent with the TextBox use of simply \r for new line and therefore create headaches later on. For example a simple replace of all \r to \n to use the result where \n is the linebreak will cause some linebreaks to be doubled (the ones included in the initial content).Erastian
W
2

Thank all.

It is working. For Silverlight TextBlock: use <lineBreak/> in the XAML of textBlock.

Thank to AnthonyWJones For Silverlight textBox, I use "\r" in the string which is used to display in TextBox.

Witkowski answered 26/1, 2011 at 3:12 Comment(0)
A
1

For a line break in a Windows Phone Silverlight TextBlock use:

"This is line one!" & vbCrLf & "This is line two!"

Anklet answered 31/1, 2012 at 0:8 Comment(0)
N
0
<TextBox x:Name="textBox" AcceptsReturn="True" />
Niki answered 24/3, 2015 at 1:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.