Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newline in a WPF-label?

Tags:

newline

label

wpf

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

<Label>Lorem    ipsum</Label> 
like image 622
Natrium Avatar asked Jan 27 '09 15:01

Natrium


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 I create a line break in TextBlock WPF?

Adding Line Breaks Sometimes you will want to insert a line break within a TextBlock. 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.

What is label in WPF?

Advertisements. The Label class provides both functional and visual support for access keys (also known as mnemonics). It is frequently used to enable quick keyboard access to controls.

What is label in XAML?

The <Label> element in XAML represents a Label control. This article shows how to use a Label control in WPF using XAML and C#. A XAML Label element represents a Label control.


2 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

like image 65
Szymon Rozga Avatar answered Oct 05 '22 22:10

Szymon Rozga


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

like image 42
00jt Avatar answered Oct 05 '22 23:10

00jt