Coder Perfect

How can I use WPF to wrap text in a label?


I’ve got a TextBox and a Label on my page. I run the following code after clicking a button:

 label1.Content = textbox1.Text; 

My issue is: how can I make the label’s text wrap? If there is too much text to fit on one line, I want it to automatically wrap to additional lines.

Asked by jeremychan

Solution #1

In WPF, the Label control does not support text wrapping natively. Instead, you should use a TextBlock. (Of course, if you want, you can put the TextBlock within a Label control.)

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.

Answered by Cody Gray

Solution #2

When utilizing the Target property (which sets focus to the targeted control when using the keyboard, e.g. ALT+C in the sample code below), you can’t always substitute a Label with a TextBlock because that’s all a Label truly offers above a TextBlock.

A Label, on the other hand, uses a TextBlock to render text (if a string is placed in the Content property, which it usually is); so, you can apply a TextBlock style to the Label as follows:

    Content="_Content Text:"
    Target="{Binding ElementName=MyTargetControl}">
        <Style TargetType="TextBlock">
            <Setter Property="TextWrapping" Value="Wrap" />
 <CheckBox x:Name = "MyTargetControl" />

You receive the capability of a Label while also being able to enclose the text in this way.

Answered by PaulJ

Solution #3

The following is the code I used.

            <AccessText TextWrapping="Wrap" Text="xxxxx"/>

Answered by bbdaffy

Solution #4

Inside the label, you can place a TextBlock:

  <TextBlock Text="Long Text . . . ." TextWrapping="Wrap" /> 

Answered by Adrian F√Ęciu

Solution #5

Change the label control’s template as follows to wrap text in the label control:

<Style x:Key="ErrorBoxStyle" TargetType="{x:Type Label}">
    <Setter Property="BorderBrush" Value="#FFF08A73"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Foreground" Value="Red"/>
    <Setter Property="Background" Value="#FFFFE3DF"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Padding" Value="5"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Top"/>
    <Setter Property="Template">
            <ControlTemplate TargetType="{x:Type Label}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true" CornerRadius="5" HorizontalAlignment="Stretch">

                    <TextBlock TextWrapping="Wrap" Text="{TemplateBinding Content}"/>


Answered by Rajesh R. Naik

Post is based on