Newline in a WPF-label?

Viewed 82246

How can I add a newline in the text of a label in WPF such as the following?

<Label>Lorem 
  ipsum</Label>
5 Answers
<Label><TextBlock>Lorem<LineBreak/>ipsum</TextBlock></Label>

You need to use TextBlock because TextBlock accepts as children a collection of Inline objects. So you are giving the TextBlock element three Inline items: Run Text="Lorem", LineBreak, and Run Text="ipsum".

You can't do the following:

<Label>Lorem<LineBreak/>ipsum</Label>`

because a label accepts one content child element.

Also, not sure exactly what your use case is but notice I placed a TextBlock inside your Label element. Is it repetitive? Not really, depending on your need. Here's a good article on the differences between the two elements: Difference between Label and TextBlock

<Label xml:space="preserve">text content
another line</Label>

seems to work too

An example of how to add a ToolTip with multiple lines to a control, such as a button. The tooltip is width limited so it will wrap if a sentence is too wide.

<!-- Button would need some properties to make it clickable.-->
<Button>
   <Button.ToolTip>
      <TextBlock Text="Line 1&#x0a;Line 2" MaxWidth="300" TextWrapping="Wrap"/>
    </Button.ToolTip>
</Button>

Tested on VS2019 + .NET 4.6.1 + WPF.

Related