Search code examples
wpftextblockunderlinetext-decorations

Text decoration underline is overriding the test for text block


I am using a text block, for which an underline is added when that text block is hovered. Everything is working well but for some alphabet like y,j the underline is not properly displayed.

<TextBlock Text="{Binding UserName}" FontSize="16" FontWeight="Bold" VerticalAlignment="Top"
                                                             Cursor="Hand" Grid.Column="0" MouseEnter="OnFocus_UserName" />

 private void OnFocus_UserName(object sender, MouseEventArgs e)
    {
        ((TextBlock) sender).TextDecorations = TextDecorations.Underline;
    }

The output is as following:

enter image description here:

How to display the underline below the anchor of y?


Solution

  • You can change the offset of the underline with the following:

     <TextBlock Text="reddy" FontSize="16" FontWeight="Bold" VerticalAlignment="Top"
                   Grid.Row="2">
            <TextBlock.TextDecorations>
                <TextDecoration PenOffset="3" Location="Underline" />
            </TextBlock.TextDecorations>
        </TextBlock>
    

    The penoffset determines how much the offset should be.

    Documentation for the PenOffset Property