27

How can I make Label text Underline in WPF? I am stucked and could not find any property for underline:

<Label Name="lblUserName"
       Content="Username"
       FontSize="14" FontWeight="Medium" />
Alex
  • 4,821
  • 16
  • 65
  • 106
Hassaan
  • 3,931
  • 11
  • 34
  • 67

3 Answers3

70

In Label no TextDecorations, therefore try this:

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

Edit: more universal solution

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

<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>
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
  • 1
    FYI, if you need to do this in the code behind (with a dynamically created component) the code is: YourTextBlockName.TextDecorations = System.Windows.TextDecorations.Underline; – Jeff Apr 15 '22 at 16:03
7

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.

David Durksen
  • 71
  • 1
  • 1
6

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>
Chris
  • 3,400
  • 1
  • 27
  • 41