How can I change the distance between text and underline in a WPF TextBlock? How can I change the distance between text and underline in a WPF TextBlock? wpf wpf

How can I change the distance between text and underline in a WPF TextBlock?


Use element syntax to add an instance of TextDecoration to the TextBlock.TextDecorations, then you can adjust the Location or PenOffset.

<TextBlock>    <TextBlock.TextDecorations>        <TextDecoration Pen="..." Location="..."/>    </TextBlock.TextDecorations></TextBlock>

(You may need to set the Pen via element syntax as well)


<TextBlock >    Here is my text to be displayed     <TextBlock.TextDecorations>        <TextDecoration PenOffset="3" PenOffsetUnit="Pixel"/>     </TextBlock.TextDecorations></TextBlock>

Adjusting PenOffset would increase/decrease the gap between text and the line.


You can do this by adding a Separator between them or by setting the Margin.

Separator:

<StackPanel Orientation="Horizontal">    <TextBlock Text="> "/>    <Separator Width="5" Visibility="Hidden" />    <TextBlock TextDecorations="Underline">        <ContentPresenter/>                            </TextBlock></StackPanel>

Margin:

<StackPanel Orientation="Horizontal">    <TextBlock Text="> " Margin="0,0,5,0" />    <TextBlock TextDecorations="Underline">        <ContentPresenter/>                            </TextBlock></StackPanel>