如何使用 WPF 将文本换行到标签中?

发布于 2024-10-17 08:58:03 字数 189 浏览 4 评论 0原文

我有一个 TextBox 和一个标签。单击按钮后,我执行以下代码:

 label1.Content = textbox1.Text; 

我的问题是,如何启用标签的文本换行?一行上可能显示太多文本,如果是这种情况,我希望它自动换行为多行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

只想待在家 2024-10-24 08:58:03

Label 控件不直接支持 WPF 中的文本换行。您应该使用 TextBlock 代替。 (当然,如果您愿意,您可以将 TextBlock 放在 Label 控件内。)

示例代码:

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

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>
巷子口的你 2024-10-24 08:58:03

我使用了以下代码。

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

I used the following code.

    <Label>
        <Label.Content>
            <AccessText TextWrapping="Wrap" Text="xxxxx"/>
        </Label.Content>
    </Label>
怎会甘心 2024-10-24 08:58:03

通常,您无法将 Label 替换为 TextBlock,因为您想要使用 Target 属性(它将焦点设置为目标< /em> 使用键盘时进行控制,例如下面示例代码中的 ALT+C),因为这就是 Label 通过 TextBlock 真正提供的全部功能。

但是,Label 使用 TextBlock 来呈现文本(如果将字符串放置在 Content 属性中,通常就是这样);因此,您可以在 Label 内添加 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" />

这样您就可以保留 Label 的功能,同时也可以能够对文本进行换行。

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.

辞慾 2024-10-24 08:58:03

您可以在标签内放置一个 TextBlock:

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

You can put a TextBlock inside the label:

<Label> 
  <TextBlock Text="Long Text . . . ." TextWrapping="Wrap" /> 
</Label> 
软糯酥胸 2024-10-24 08:58:03

要在标签控件中换行文本,请更改标签的模板,如下所示:

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

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>
小伙你站住 2024-10-24 08:58:03

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

您始终可以这样做:

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

但是,如果所有这些“标签”都是为了显示文本,请改用 TextBlock

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.

独行侠 2024-10-24 08:58:03

我们需要放置某种可以包裹文本的控件,例如文本块/文本框

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

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>
第七度阳光i 2024-10-24 08:58:03

尝试使用这个

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

try use this

lblresult.Content = lblresult.Content + "prime are :" + j + "\n";
×纯※雪 2024-10-24 08:58:03
 <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>
 <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>
眼藏柔 2024-10-24 08:58:03

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

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

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;
醉城メ夜风 2024-10-24 08:58:03

试试我的版本

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

没有必要使用 ScrollViewer 控件。

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.

芯好空 2024-10-24 08:58:03

其他需要记住的是父容器、 等等。受影响的需要相应调整,否则换行不起作用。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文