Is there a way to have \n
make a line break in a TextBlock
?
<TextBlock Text="line1\nLine2" />
Or is there a better way to force a middle line break, inside the Text
attribute?
<LineBreak />
This doesn't work for me, it needs to be the value of the Text
attribute, because the text string is being set from an outside source.
I'm familiar with LineBreak
but it's not the answer I'm looking for.
Adding Line Breaks Sometimes you will want to insert a line break within a TextBlock. You can do this with a LineBreak inline, added as an XAML element within the text. A new line will be started at the position of this element.
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 
 , for \r\n you'd use 
 and so on.
The \n or the \r escape character in Mac is used to add a new line to the console in C#. For a Windows machine, we should use the \n escape character for line breaks.
To draw a line, create a Line element. Use its X1 and Y1 properties to set its start point; and use its X2 and Y2 properties to set its end point. Finally, set its Stroke and StrokeThickness because a line without a stroke is invisible. Setting the Fill element for a line has no effect, because a line has no interior.
Try this:
<TextBlock> line1 <LineBreak /> line2 </TextBlock>
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