title: How to make a WPF control that updates every second?
body: <p>This question is similar to <a href="https://stackoverflow.com/questions/949458/">another one I asked</a>, but whereas in that case I wanted to know about forcing a binding to update from XAML or a view model, in this case I'd like to know about wrapping this up into a custom WPF <code>FrameworkElement</code>.</p>
  
  <p>The functionality I'm after is a span of text that indicates how long ago something happened.</p>
  
  <pre><code>&lt;TextBlock Text="It happened " /&gt;
  &lt;my:AgeTextBlock SinceTime="{Binding OccurredAtUtc}" /&gt;
  &lt;TextBlock Text=" ago" /&gt;
  </code></pre>
  
  <p>This would render as (for example):</p>
  
  <blockquote>
    <p><em>It happened 1 min 13 sec ago</em></p>
  </blockquote>
  
  <p>I have code that converts from a <code>TimeSpan</code> to the human-readable form shown.</p>
  
  <p>In order to have the UI update every second I'm considering using a static <code>DispatcherTimer</code> (idea from <a href="https://stackoverflow.com/questions/949458/how-to-have-a-wpf-binding-update-every-second/949616#949616">Kent Boogaart's answer</a>).</p>
  
  <p>So here's what I have:</p>
  
  <pre><code>public class AgeTextBlock : TextBlock
  {
      private static readonly DispatcherTimer _timer;
  
      static AgeTextBlock()
      {
          _timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
      }
  
      public AgeTextBlock()
      {
          _timer.Tick += ...;
      }
  
      // How can I unsubscribe from the Tick event to avoid a memory leak?
  }
  </code></pre>
  
  <p>The comment indicates my problem.  I can't see how I'd clean up properly with this approach.  There's no <code>Dispose</code> method to override and remove the event handler.</p>
  
  <p>What is the recommended pattern here?</p>