• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2887
  • Last Modified:

How to have more than one line of text in WPF label or text box

I am brand new to WPF controls. I need to create labels with more than one line of text and I have been having trouble figuring that out.

Thanks in advance for your help.
0
kyleboca
Asked:
kyleboca
  • 5
  • 3
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
*Assuming your control is tall enough to display multiple lines...

You could do:

    this.label1.Content = "Line 1\r\nLine 2\r\nLine 3\r\n";

Or:

    this.label1.Content = "Line 1" + Environment.NewLine + "Line 2" + Environment.NewLine + "Line 3" + Environment.NewLine;
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
VB.Net also has the old vbCrLf constant:

        Me.Label1.Content = "Line 1" & vbCrLf & "Line 2" & vbCrLf & "Line 3" & vbCrLf
0
 
kylebocaAuthor Commented:
I meant in the IDE property area next to content in the properties window.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Oh...hehe.  Try pressing Alt-Enter to go down to the next line.
0
 
kylebocaAuthor Commented:
alt+enter didn't work either.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hmm...the WPF IDE works differently than the WinForms IDE...sorry about that!  =\

For Labels, you can select thim on the form then edit the XAML directly by adding a TextBlock as the Content and using :

       
            Line 1Line 2Line 3
       

Here is a picture:
WPF-MultiLine-Label-Using-TextBl.jpg
0
 
kylebocaAuthor Commented:
That looks like what I need to learn. Is there a way to "center" the multi-lines of text withing the label using xaml code?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Just set the HorizonalContentAlignment and VerticalContentAlignment properties to Center.  This can be done thru the Properties Pane or you can enter it manually in the XAML:

       
            Line 1Line 2Line 3
       
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now