如何使用WPF将文本包装在标签中?

时间:2022-11-29 11:05: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 个解决方案

#1


305  

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

标签控件不直接支持WPF中的文本包装。你应该使用一个文本块。(当然,如果您愿意,可以将TextBlock放在Label控件的内部。)

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>

#2


92  

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.

通常你不能用文本块替换标签,因为你想要使用目标属性(它在使用键盘时将焦点放在目标控件上,例如下面示例代码中的ALT+C),因为这是标签在文本块上真正提供的所有功能。

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:

但是,一个标签使用一个TextBlock来呈现文本(如果一个字符串被放置在内容属性中,它通常是这样);因此,可以在标签内添加文本块的样式如下:

<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


68  

I used the following code.

我使用了以下代码。

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

#4


27  

You can put a TextBlock inside the label:

你可以在标签内加上一个TextBlock:

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

#5


13  

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>

#6


10  

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

我建议使用TextBlock而不是Label类。这允许您适当地设置TextWrapping。

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.

然而,如果所有这些“标签”都是用来显示文本的,那么使用一个TextBlock代替。

#7


3  

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>

#8


2  

try use this

尝试使用这个

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

#9


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>

#10


0  

I used this for retrieving data from MySql Database:

我用它从MySql数据库中检索数据:

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

#1


305  

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

标签控件不直接支持WPF中的文本包装。你应该使用一个文本块。(当然,如果您愿意,可以将TextBlock放在Label控件的内部。)

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>

#2


92  

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.

通常你不能用文本块替换标签,因为你想要使用目标属性(它在使用键盘时将焦点放在目标控件上,例如下面示例代码中的ALT+C),因为这是标签在文本块上真正提供的所有功能。

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:

但是,一个标签使用一个TextBlock来呈现文本(如果一个字符串被放置在内容属性中,它通常是这样);因此,可以在标签内添加文本块的样式如下:

<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


68  

I used the following code.

我使用了以下代码。

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

#4


27  

You can put a TextBlock inside the label:

你可以在标签内加上一个TextBlock:

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

#5


13  

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>

#6


10  

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

我建议使用TextBlock而不是Label类。这允许您适当地设置TextWrapping。

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.

然而,如果所有这些“标签”都是用来显示文本的,那么使用一个TextBlock代替。

#7


3  

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>

#8


2  

try use this

尝试使用这个

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

#9


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>

#10


0  

I used this for retrieving data from MySql Database:

我用它从MySql数据库中检索数据:

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