title: Underline text in WPF
body: <p>I have an WPF usercontrol that has a TextBox. I set the text <code>Underline</code> or not, <code>Strikethrough</code> or not. </p>
  
  <p>I think something is wrong with my code, especially <em>unset</em> part, because is not working properly every time... probably some elements of Underline are removed with Strikethrough?...</p>
  
  <p>Especiallly when setting the same property multiple times to the same value, it adds and adds....</p>
  
  <pre><code>private void UpdateUnderline()
  {
      if (this.IsUnderline)
      {
          this.MyTextBlock.TextDecorations.Add(TextDecorations.Underline);
      }
      else
      {
          foreach (var item in TextDecorations.Underline)
          {
              this.MyTextBlock.TextDecorations.Remove(item);
          }
      }
  }
  
  private void UpdateStrikethrough()
  {
      if (this.IsStrikethrough)
      {
          this.MyTextBlock.TextDecorations.Add(TextDecorations.Strikethrough);
      }
      else
      {
          foreach (var item in TextDecorations.Strikethrough)
          {
              this.MyTextBlock.TextDecorations.Remove(item);
          }
      }
  }
  </code></pre>
  
  <p>as I can't do </p>
  
  <pre><code>this.MyTextBlock.TextDecorations.Contains(TextDecorations.Strikethrough);
  </code></pre>
  
  <p>it adds and adds, and adds again, so when it removes...it remains :)</p>
  
  <h1>Example:</h1>
  
  <pre><code>&lt;Window x:Class="WpfApplication10.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Grid&gt;
          &lt;StackPanel&gt;
              &lt;CheckBox Content="Underline"
                        IsChecked="{Binding IsUnderline}" /&gt;
              &lt;CheckBox Content="Strikethrough"
                        IsChecked="{Binding IsStrikethrough}" /&gt;
              &lt;TextBlock Name="MyTextBlock"
                         Text="Quick Brown Fox Jumps Over The Lazy Dog." /&gt;
              &lt;Button Content="Underline" Name="Underline" Click="Underline_Click"/&gt;
              &lt;Button Content="Strikethrough" Name="Strikethrough" Click="Strikethrough_Click"/&gt;
              &lt;Button Content="NO Underline" Name="NoUnderline" Click="NoUnderline_Click"/&gt;
              &lt;Button Content="NO Strikethrough" Name="NoStrikethrough" Click="NoStrikethrough_Click" /&gt;
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>cs:</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      public MainWindow()
      {
          DataContext = this;
      }
  
      private Boolean _IsUnderline;
      public Boolean IsUnderline
      {
          get
          {
              return _IsUnderline;
          }
          set
          {
              _IsUnderline = value;
              UpdateUnderline();
          }
      }
      private Boolean _IsStrikethrough;
      public Boolean IsStrikethrough
      {
          get
          {
              return _IsStrikethrough;
          }
          set
          {
              _IsStrikethrough = value;
              UpdateStrikethrough();
          }
      }
  
      private void UpdateUnderline()
      {
          if (this.IsUnderline)
          {
              this.MyTextBlock.TextDecorations.Add(TextDecorations.Underline);
          }
          else
          {
              foreach (var item in TextDecorations.Underline)
              {
                  this.MyTextBlock.TextDecorations.Remove(item);
              }
          }
      }
  
      private void UpdateStrikethrough()
      {
          if (this.IsStrikethrough)
          {
              this.MyTextBlock.TextDecorations.Add(TextDecorations.Strikethrough);
          }
          else
          {
              foreach (var item in TextDecorations.Strikethrough)
              {
                  this.MyTextBlock.TextDecorations.Remove(item);
              }
          }
      }
  
      private void Underline_Click(object sender, RoutedEventArgs e)
      {
          IsUnderline = true;
      }
  
      private void Strikethrough_Click(object sender, RoutedEventArgs e)
      {
          IsStrikethrough = true;
      }
  
      private void NoUnderline_Click(object sender, RoutedEventArgs e)
      {
          IsUnderline = false;
      }
  
      private void NoStrikethrough_Click(object sender, RoutedEventArgs e)
      {
          IsStrikethrough = false;
      }
  }
  </code></pre>