How can I add a newline in the text of a label in WPF such as the following?
<Label>Lorem
ipsum</Label>
How can I add a newline in the text of a label in WPF such as the following?
<Label>Lorem
ipsum</Label>
<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
in WPF you can use the value " "
or "
"
For example:
<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)
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";
I understand that newlines work in a textblock. However if you want a newline in a label, as the question stated, use this in your string. I have spaces between because otherwise it will not allow me to post the sequence in this comment. & # x a ; Use these 5 chars together without spaces to get the newline you're after.