Adding new line to text property of a label box
In WPF, you can add a new line to the Text
property of a Label
control by using the \r\n
or Environment.NewLine
syntax.
Here are a few examples:
- Using
\r\n
:<Label Content="Line 1\r\nLine 2" />
This will render as:
Line 1 Line 2
- Using
Environment.NewLine
:<Label Content="Line 1{Environment.NewLine}Line 2" />
This will also render as:
Line 1 Line 2
- Using a
TextBlock
instead of aLabel
:<TextBlock Text="Line 1" TextWrapping="Wrap"> <Run Text="Line 2" /> </TextBlock>
This will also render as:
Line 1 Line 2
Note that the TextWrapping
property is set to Wrap
to allow the text to wrap to a new line.
- Using a
StringBuilder
to build the text:string text = "Line 1"; text += Environment.NewLine; text += "Line 2";
Label myLabel = new Label(); myLabel.Content = text;
This will also render as:
Line 1
Line 2