【问题标题】:How to make Label Text Underline?如何使标签文本下划线?
【发布时间】:2014-05-01 19:20:47
【问题描述】:

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

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

【问题讨论】:

    标签: wpf xaml label underline


    【解决方案1】:

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

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

    Edit: more universal solution

    在这种情况下,不要使用Label.Tag,而不是Label.Content,因为 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>
    

    【讨论】:

      【解决方案2】:

      这是一个关于样式的答案。

      内容:

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

      【讨论】:

        【解决方案3】:

        这是一种将样式直接应用于标签的方法:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-06-06
          • 2012-02-26
          • 2014-06-11
          • 2017-11-19
          • 2015-02-07
          • 2015-08-08
          • 1970-01-01
          相关资源
          最近更新 更多