jeremychan
jeremychan

Reputation: 4459

How can I wrap text in a label using WPF?

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.

Upvotes: 304

Views: 292075

Answers (12)

Michael W. Powell
Michael W. Powell

Reputation: 306

Something else to keep in mind is parent containers, <Grid/> and what not. Impacted <ColumnDefinition/> needs to be adjusted accordingly, or the wrapping has no effect.

<Grid>
  <Grid.ColumnDefinitions>
    <!-- auto prohibits wrapping -->
    <ColumnDefinition Width="auto" />
    <!-- whereas the Kleene star allows wrapping -->
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>
</Grid>

Upvotes: 0

antonio
antonio

Reputation: 718

Try my version

<Label x:Name="myLabel">
        <TextBox FontSize="20" FontWeight="Bold" Background="Transparent" BorderBrush="Transparent"
                 IsReadOnly="true" TextWrapping="Wrap" HorizontalAlignment="Center" Focusable="False"
                 Foreground="{Binding Foreground, ElementName=myLabel}"
            Text="longMeeeeessssagggeeeeeeeeeeeeeeeeeeeeeeeeeee_Ooooooooeeeeeeeeeee" />
</Label>

Not necessary to use ScrollViewer control.

Upvotes: 0

Rajesh R. Naik
Rajesh R. Naik

Reputation: 203

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>

Upvotes: 20

DiogoA.
DiogoA.

Reputation: 53

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;

Upvotes: 1

PaulJ
PaulJ

Reputation: 2979

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.

Upvotes: 122

Khalid
Khalid

Reputation: 645

 <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>

Upvotes: 2

Kylo Ren
Kylo Ren

Reputation: 8803

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>

Upvotes: 6

Adrian F&#226;ciu
Adrian F&#226;ciu

Reputation: 12552

You can put a TextBlock inside the label:

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

Upvotes: 40

RathapongPumpo
RathapongPumpo

Reputation: 21

try use this

lblresult.Content = lblresult.Content + "prime are :" + j + "\n";

Upvotes: 2

bbdaffy
bbdaffy

Reputation: 1361

I used the following code.

    <Label>
        <Label.Content>
            <AccessText TextWrapping="Wrap" Text="xxxxx"/>
        </Label.Content>
    </Label>

Upvotes: 128

Cody Gray
Cody Gray

Reputation: 244672

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>

Upvotes: 460

Reed Copsey
Reed Copsey

Reputation: 564323

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.

Upvotes: 13

Related Questions