wpf 如何使标签文本下划线?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22609341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:04:35  来源:igfitidea点击:

How to make Label Text Underline?

wpfxamllabelunderline

提问by Hassaan

How can I make Labeltext Underlinein WPF? I am stucked and could not find any property for underline:

如何在 WPF 中制作Label文本Underline?我被困住了,找不到下划线的任何属性:

<Label Name="lblUserName"
       Content="Username"
       FontSize="14" FontWeight="Medium" />

回答by Anatoliy Nikolaev

In Labelno TextDecorations, therefore try this:

Label没有TextDecorations,因此试试这个:

<Label Width="100" Height="30">
    <TextBlock TextDecorations="Underline">TestText</TextBlock>
</Label>

Edit: more universal solution

Edit: more universal solution

In this case, instead of Label.Contentusing Label.Tag, because Content property can be set only once:

在这种情况下,不要Label.Content使用Label.Tag,因为 Content 属性只能设置一次:

<Label Tag="TestContent" 
       Width="100" 
       Height="30"
       HorizontalContentAlignment="Center"
       Background="AliceBlue">

    <TextBlock TextDecorations="Underline" 
               Text="{Binding Path=Tag, 
                              RelativeSource={RelativeSource Mode=FindAncestor,
                                                             AncestorType={x:Type Label}}}" />
</Label>

回答by Chris

Here's an answer with styles.

这是样式的答案。

Content:

内容:

<Label>
    <TextBlock Style="{DynamicResource StyleName}">text content</TextBlock>
</Label>

And the style:

和风格:

<Style x:Key="StyleName">
    <Setter Property="TextBlock.TextDecorations" Value="Underline" />
    <Setter Property="TextBlock.FontStyle" Value="Italic" />
</Style>

回答by David Durksen

Here's a way to apply the style directly to the Label:

下面是一种将样式直接应用到标签的方法:

<Style TargetType="Label">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock Text="{Binding}" TextDecorations="Underline"/>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

This simplifies the label items:

这简化了标签项:

<Label>
    Label 1
</Label>

<Label Grid.Row="1">
    Label 2
</Label>

This works if the content of the labels text only.

如果仅标签文本的内容,则此方法有效。