Fill the underline decoration with a solid color brush in C#

image_pdfimage_print


   
  

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="TextDecorationExample.Window1"
  Title="TextDecoration Example"
  Width="720"
  Height="400"
  Loaded="WindowLoaded">
  <StackPanel>
      <TextBlock Name="underlineTextBlock" FontSize="24" Width="180" VerticalAlignment="Center">The lazy dog</TextBlock>


  </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Media;

namespace TextDecorationExample
{
    public partial class Window1 : Window
    {
         private void WindowLoaded(object sender, EventArgs e)
         {

             TextDecorationCollection myCollection = new TextDecorationCollection();
             TextDecoration myUnderline = new TextDecoration();
             myUnderline.Location = TextDecorationLocation.Underline;

             // Set the solid color brush.
             myUnderline.Pen = new Pen(Brushes.Red, 1);
             myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;

             // Set the underline decoration to the text block.
             myCollection.Add(myUnderline);
             underlineTextBlock.TextDecorations = myCollection;
         }

    }
}