jeremychan jeremychan - 1 year ago 218
C# Question

How can I wrap text in a label using WPF?

I have a

and a Label. After clicking a button, I execute the following code:

label1.Content = textbox1.Text;

My question is, how do I enable text wrapping of the label? There may be too much text to display on one line, and I want it to automatically wrap to multiple lines if that is the case.

Answer Source

The Label control doesn't directly support text wrapping in WPF. You should use a TextBlock instead. (Of course, you can place the TextBlock inside of a Label control, if you wish.)

Sample code:

<TextBlock TextWrapping="WrapWithOverflow">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec adipiscing
    nulla quis libero egestas lobortis. Duis blandit imperdiet ornare. Nulla
    ac arcu ut purus placerat congue. Integer pretium fermentum gravida.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download