如何使标签文字下划线?

问题描述 投票:12回答:3

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

<Label Name="lblUserName"
       Content="Username"
       FontSize="14" FontWeight="Medium" />
wpf xaml label underline
3个回答
43
投票

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

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

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>

3
投票

这是风格的答案。

内容:

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

风格:

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

0
投票

这是将样式直接应用于Label的方法:

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

这简化了标签项:

<Label>
    Label 1
</Label>

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

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

© www.soinside.com 2019 - 2024. All rights reserved.