#813 – Applying TextDecorations in the Middle of a Block of Text

You can apply one of several different text decorations to a block of text by using the TextDecorations property for a TextBlock control.  But setting this property means that the decoration chosen applies to all text contained within the TextBlock.

Alternatively, you can apply one of the four text decorations (Underline, Baseline, StrikeThrough or Overline) to a subset of the text by using a Run element.  (Note: In the example below, we use an Underline tag directly, rather than setting the TextDecorations property on a Run element).

        <TextBlock Padding="20,10" FontSize="16"
                   TextWrapping="Wrap">
            We <Underline>few</Underline>,
            we <Run TextDecorations="Baseline">happy</Run> few,
            we band of <Run TextDecorations="StrikeThrough">guys</Run> brothers;
            For he to-day that sheds his <Run TextDecorations="Overline">blood</Run> with me
            Shall be my brother; be he ne'er so vile,
            This day shall gentle his condition;"
        </TextBlock>

813-001

Advertisement

#812 – Use TextDecorations Property to Underline or Strike Through Text

Some controls that render text include a TextDecorations property that allows you to underline the text or draw a line through the middle of the text.

The allowed values of the TextDecorations property are:

  • Underline – Underline the text (line appears just a bit below the bottom of the characters)
  • Baseline – Draw a line that runs along the bottom edge of the characters (just above where the underline would appear)
  • StrikeThrough – Draw a line through the middle of the characters
  • Overline – Draw a line over the top edge of the characters

Below is an example of the TextDecorations property used with the TextBlock control.  TextDecorations is not supported for Labels.

        <TextBlock Text="Underline pphh" Padding="20,10" FontSize="16"
                   TextDecorations="Underline"/>
        <TextBlock Text="Baseline pphh" Padding="20,10" FontSize="16"
                   TextDecorations="Baseline"/>
        <TextBlock Text="StrikeThrough pphh" Padding="20,10" FontSize="16"
                   TextDecorations="StrikeThrough"/>
        <TextBlock Text="Overline pphh" Padding="20,10" FontSize="16"
                   TextDecorations="Overline"/>

812-001