Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you force a line break between two words in a XAML-declared Label? [duplicate]

Tags:

wpf

xaml

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&#x0a;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&#x0a;Break:" />
</Label>

Thanks to Julien for the idea of using a TextBlock.

like image 242
devuxer Avatar asked Aug 01 '09 07:08

devuxer


People also ask

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.

How do you draw a line in XAML?

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.

How do I create a line break in Textblock WPF?

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.


3 Answers

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.

like image 85
Julien Poulin Avatar answered Oct 14 '22 01:10

Julien Poulin


If you want a new line in a label:

<Label Content="Lorem &#10;ipsum" />

("10" is the ascii number for newline)

or

<Label Content="Lorem &#xA;ipsum" />

("A" is the ascii number for newline in hex)

like image 45
Tabrez Shaikh Avatar answered Oct 13 '22 23:10

Tabrez Shaikh


I'd do this:

<StackPanel>
    <Label>First line</Label>
    <Label>Second line</Label>
</StackPanel>

If the formatting gets really involved, I'd use FlowDocumentScrollViewer.

like image 1
Daniel Earwicker Avatar answered Oct 13 '22 23:10

Daniel Earwicker