Maybe I'm not using the right key words, but all my searches are coming up empty. How do you force a line break?
I can tell you that none of the following work:
<Label
Content="Line&br;Break:" />
<Label
Content="Line<br />Break:" />
<Label
Content="Line
Break:" />
<Label
Content="Line\nBreak:" />
Can someone share this closely guarded secret?
Thanks.
EDIT:
Okay, never mind. I finally found it.
<Label
Content="Line
Break:" />
Definitely not easy to guess!
EDIT 2:
Okay, and now to get the text to be right-justified, I went with this:
<Label>
<TextBlock
TextAlignment="Right"
Text="Line
Break:" />
</Label>
Thanks to Julien for the idea of using a TextBlock.
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.
Creating a LineThe Line element in XAML creates a line shape. The following code snippet creates a Line by setting its start point (X1, Y1) to (50, 50) and end point (X2, Y2) to (200, 200). That means a line is drawn from point (50, 50) to (200, 200). The code also sets the color of the line to red and width 4.
Adding Line Breaks 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. If you have configured the text to be justified, the final line before the line break will not be expanded to fill the available width.
If you only need to display text, you can use a TextBlock
instead of a Label
:
<TextBlock>
Line<LineBreak/>Break:
</TextBlock>
If you really need a Label
(e.g. you need to respond to a click event), you can wrap the above code inside a Label
.
If you want a new line in a label:
<Label Content="Lorem ipsum" />
("10" is the ascii number for newline)
or
<Label Content="Lorem 
ipsum" />
("A" is the ascii number for newline in hex)
I'd do this:
<StackPanel>
<Label>First line</Label>
<Label>Second line</Label>
</StackPanel>
If the formatting gets really involved, I'd use FlowDocumentScrollViewer
.
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