title: Where to unsubcribe from an event for a custom control
body: <p>I am wondering at what point do I unsubscribe from an event, up to this point I was usually unsubscribing on the line before subscribing (or most of the time the event was called from xaml, which then is handled by xaml and there is no need to do any extra work). 
  But now I'm in a situation when I want to subscribe at the constructor so, where do I unsubscribe? I tried to do it inside unloaded event, 
  but my control is often unloaded and then loaded again without recreating it.</p>
  
  <p><strong>Edit</strong> 
  To make it clear I want to unsubscribe when the object is not needed anymore, I was hoping that there is Dispose method I can override or something like this.
  Any ideas?</p>
  
  <p>Sample code </p>
  
  <pre><code>public class MYListBox : ListBox
  {
      public MYListBox()
      {
          SelectionChanged += MYListBox_SelectionChanged;
          Unloaded += MYListBox_Unloaded;
      }
  
      private void MYListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
      }
  
      private void MYListBox_Unloaded(object sender, RoutedEventArgs e)
      {
          SelectionChanged -= MYListBox_SelectionChanged;
          Unloaded -= MYListBox_Unloaded;
      }
  }
  </code></pre>
  
  <p>xaml </p>
  
  <pre><code>&lt;UserControl x:Class="WpfApplication1.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:wpfApplication1="clr-namespace:WpfApplication1"&gt;
  &lt;wpfApplication1:MYListBox /&gt;
  &lt;/UserControl &gt;
  </code></pre>
  
  <p>So sometimes, I will allow the user navigate somewhere else and then allow it to comeback to the same instance. Another time when the user navigates when he comes back it will be a new instance. So at the second situation I thought I need to unsubcribe from that event. </p>
  
  <p>Thank you :)</p>