296

I have a TextBox 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.

10 Answers 10

451

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.
</TextBlock>
4
  • 8
    This is fine, but not if you want to use the Target property feature of the Label--which to be fair is probably the only reason you'd use a Label over a TextBlock. I have provided an answer to this question that show's how to get wrapping in a Label (below)
    – PaulJ
    Mar 2, 2012 at 11:04
  • 25
    JulianM - they already readonly stackoverflow.com/questions/5073244/making-textblock-readonly ? Maybe they weren't in 2011 May 27, 2014 at 23:02
  • 16
    @JulianM are you confusing TextBlock with TextBox? -- TextBlock's have been read-only since WPF was code named "Avalon". Apr 20, 2016 at 20:09
  • I needed to set Padding="5" to match the padding in my other Label controls on the form. See Rajesh's answer below. Jul 21, 2018 at 18:34
119

Often you cannot replace a Label with a TextBlock as you want to the use the Target property (which sets focus to the targeted control when using the keyboard e.g. ALT+C in the sample code below), as that's all a Label really offers over a TextBlock.

However, a Label uses a TextBlock to render text (if a string is placed in the Content property, which it typically is); therefore, you can add a style for TextBlock inside the Label like so:

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

This way you get to keep the functionality of a Label whilst also being able to wrap the text.

3
  • 3
    Does this actually work? From what I've seen the textblock that WPF adds does not exist in the logical tree and will not pick up your resource.
    – Gusdor
    Aug 12, 2013 at 8:08
  • 2
    Yes, this does work—I tested it before I published my answer. You are right in that the elements targeted are not in the logical tree, but it's the visual tree that matters here.
    – PaulJ
    Aug 16, 2013 at 13:15
  • 14
    This is a few years old but this does not seem to work with VS 2015. What does seem to work is using a TextBlock in a label <Label> <TextBlock TextWrapping="Wrap" Text="Whatever" /> </Label> Apr 30, 2016 at 23:02
118

I used the following code.

    <Label>
        <Label.Content>
            <AccessText TextWrapping="Wrap" Text="xxxxx"/>
        </Label.Content>
    </Label>
4
  • 7
    This is what worked best for me. Simple, no need to use other controls or add much code.
    – Yisela
    Mar 19, 2014 at 0:28
  • 5
    @jeremychan, I would mark this as the answer. I am happy that I scrolled down and seen this answer.
    – Mehrad
    Dec 11, 2014 at 0:04
  • I revert my up vote.. I tried using this but it by-passed my "HorizontalAlignment="Center"". Unlike using Textblock, it didn't...
    – Jack Frost
    Feb 9, 2016 at 6:26
  • 6
    The Label.Content part is unnecessary, as anything within the label tag is automatically content. Nonetheless, an excellent answer as it retains the ability to provide access keys, where all the solutions using a TextBlock fail in this regard.
    – Rob G
    Sep 7, 2016 at 12:11
40

You can put a TextBlock inside the label:

<Label> 
  <TextBlock Text="Long Text . . . ." TextWrapping="Wrap" /> 
</Label> 
1
  • 4
    I like this approach because it allows your Label styles to apply to this textblock without having to make additional styles for the TextBlock.
    – Sogger
    Jan 8, 2013 at 17:27
19

To wrap text in the label control, change the the template of label as follows:

<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">
        <Setter.Value>
            <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}"/>
                </Border>
                    
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
2
  • Addendum: if you need to add text wrapping or other TextBlock-specific properties you can mix-in with the following recipe stackoverflow.com/questions/3970285/… Oct 14, 2015 at 11:16
  • Be careful with this answer. A normal Label can have a control as its content but this one doesn't allow it.
    – Ostas
    Sep 14 at 19:55
13

Instead of using a Label class, I would recommend using a TextBlock. This allows you to set the TextWrapping appropriately.

You can always do:

 label1.Content = new TextBlock() { Text = textBox1.Text, TextWrapping = TextWrapping.Wrap };

However, if all this "label" is for is to display text, use a TextBlock instead.

1
  • I ended up using this method for populating dynamic label contents. Seems to be the easiest and cleanest method. I imagine you could incorporate the line into the label1 definition, i.e. <br/> Label label1 = new Label() { Content = new TextBlock() { Text = caption, TextWrapping = TextWrapping.Wrap }, FontWeight = FontWeights.Normal };
    – dashrader
    Dec 13, 2019 at 19:42
6

We need to put some kind of control that can wrap text like textblock/textbox

 <Label Width="120" Height="100" >
        <TextBlock TextWrapping="Wrap">
            this is a very long text inside a textblock and this needs to be on multiline.
        </TextBlock>
    </Label>
2

try use this

lblresult.Content = lblresult.Content + "prime are :" + j + "\n";
2
 <Label x:Name="datetimeofmsg" 
           HorizontalAlignment="Left" Margin="4.286,55,0,0" 
           VerticalAlignment="Top" Background="{x:Null}" 
           FontWeight="Bold" Width="61.714" Height="20" Foreground="White">
        <Label.Content>
            <AccessText TextWrapping="Wrap"/>
        </Label.Content>
    </Label>
2

I used this to retrieve data from MySql Database:

AccessText a = new AccessText();    
a.Text=reader[1].ToString();       // MySql reader
a.Width = 70;
a.TextWrapping = TextWrapping.WrapWithOverflow;
labels[i].Content = a;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.