tags:

views:

625

answers:

2

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.

+4  A: 

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.

Julien Poulin
I don't need it to respond to a click event, but I do need it to gray out when disabled (which I believe TextBlock won't do without extra code/markup). However, I'm now having a problem trying to right justify the text of my label, so I might need to use something else. HorizontalAlignment="Right" and HorizontalContentAlignment="Right" both have no effect.
DanM
You can set the TextAlignment property of the TextBlock to Right. As for the gray out, it's going to require extra markup.
Julien Poulin
Thanks, Julien. I was able to solve by putting the TextBlock inside a Label (see my edits above).
DanM
+1  A: 

I'd do this:

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

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

Daniel Earwicker
This is not the intended way. Because they're separate controls, you have to deal with margins etc... (it acts more like a paragraph)The TextBlock with LineBreaks is much cleaner!
Jo-wen