100

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

<Label>Lorem 
  ipsum</Label>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Natrium
  • 30,772
  • 17
  • 59
  • 73

5 Answers5

132

in WPF you can use the value "&#10;" or "&#xA;"

For example:

<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)

Example, with a border arround label to show boundry

00jt
  • 2,818
  • 3
  • 25
  • 29
  • 4
    Agreed this should be the accepted answer. It's more elegant and simpler to use. – Tyler C Jun 16 '17 at 13:05
  • 2
    This works fine if put straight into the XAML, but does not seem to work when put into a resource string - it just displays Lorem Ipsum in the label. As @stijn pointed out, this is what is required for localisation, but it doesn't work. – Dave Jan 18 '22 at 12:24
126
<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

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Szymon Rozga
  • 17,971
  • 7
  • 53
  • 66
  • 1
    When I tried the answer above, I got an error for having a tag in the middle of my content. Instead, I used this: ` First Second ` – Pathogen Apr 18 '12 at 20:53
  • 4
    This works great but is not useful when using localization. In that case use `Lorem Ipsum` – stijn May 30 '13 at 07:17
12

When doing this in the ViewModel or Model, I have found that using Environment.NewLine has the most consistent outcome, including localization. It should work directly in the View as well, but I haven't tested that.

Example:

In the View

<Label Content="{Binding SomeStringObject.ParameterName}" />

In the ViewModel:

SomeStringObject.ParameterName = "First line" + Environment.NewLine + "Second line";
HeyZiko
  • 1,660
  • 15
  • 28
3

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.

Contango
  • 76,540
  • 58
  • 260
  • 305
3
<Label xml:space="preserve">text content
another line</Label>

seems to work too

Karl
  • 3,170
  • 1
  • 21
  • 28
  • This does what the question asks, but one caveat is that it also preserves whitespace. So "nicely formatted" text in XAML will include all those indenting spaces, too. – Raphael Schmitz Mar 10 '23 at 12:48