How can I wrap text in a label using WPF?

2019-01-10 18:22发布

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条回答
家丑人穷心不美
2楼-- · 2019-01-10 18:24

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>
查看更多
▲ chillily
3楼-- · 2019-01-10 18:27

You can put a TextBlock inside the label:

<Label> 
  <TextBlock Text="Long Text . . . ." TextWrapping="Wrap" /> 
</Label> 
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-10 18:27

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.

查看更多
你好瞎i
5楼-- · 2019-01-10 18:27

I used this for retrieving 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;
查看更多
够拽才男人
6楼-- · 2019-01-10 18:29

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>
查看更多
祖国的老花朵
7楼-- · 2019-01-10 18:34

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>
查看更多
登录 后发表回答