如何使标签文字下划线?

Has*_*aan 14 wpf xaml label underline

如何在WPF中创建Label文本Underline?我被困了,找不到下划线的任何财产:

<Label Name="lblUserName"
       Content="Username"
       FontSize="14" FontWeight="Medium" />
Run Code Online (Sandbox Code Playgroud)

Ana*_*aev 45

Label没有TextDecorations,所以试试这个:

<Label Width="100" Height="30">
    <TextBlock TextDecorations="Underline">TestText</TextBlock>
</Label>
Run Code Online (Sandbox Code Playgroud)

Edit: more universal solution

在这种情况下,而不是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>
Run Code Online (Sandbox Code Playgroud)


Chr*_*ris 5

这是样式的答案。

内容:

<Label>
    <TextBlock Style="{DynamicResource StyleName}">text content</TextBlock>
</Label>
Run Code Online (Sandbox Code Playgroud)

和风格:

<Style x:Key="StyleName">
    <Setter Property="TextBlock.TextDecorations" Value="Underline" />
    <Setter Property="TextBlock.FontStyle" Value="Italic" />
</Style>
Run Code Online (Sandbox Code Playgroud)


小智 5

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

<Style TargetType="Label">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock Text="{Binding}" TextDecorations="Underline"/>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

这简化了标签项:

<Label>
    Label 1
</Label>

<Label Grid.Row="1">
    Label 2
</Label>
Run Code Online (Sandbox Code Playgroud)

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