title: In WPF, how to add an EventHandler for a FrameworkElement designed in Template?
body: <p>I have defined the following <code>DataTemplate</code> for <code>ListBox</code> items in an external resource dictionary: </p>
  
  <pre><code>&lt;DataTemplate x:Key="MyListBoxItemTemplate" DataType="{x:Type entities:Track}"&gt;
      &lt;StackPanel&gt;       
          &lt;TextBlock Text="Here's the slider:" /&gt;
          &lt;Slider Name="MySlider" Height="23" Minimum="0" /&gt;
      &lt;/StackPanel&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>I need to provide an event handler method for Slider's <code>ValueChanged</code> event. I don't know where am I supposed to write that code as it is impractical to specify event handler for a control within a template. </p>
  
  <p>I've been googling for the solution and found that I should add the event handler in the override of the <code>OnApplyTemplate()</code> method. My guess is that it should look something like this or similar: </p>
  
  <pre><code>public override void OnApplyTemplate()
  {
      base.OnApplyTemplate();
      // Is the following initialization even going to work!?!?
      Slider MySlider = this.FindName("MySlider") as Slider;
      SeekSlider.ValueChanged += 
          new RoutedPropertyChangedEventHandler&lt;double&gt;(SeekSlider_ValueChanged);
  }
  </code></pre>
  
  <p>But where should I write this method? Does OnApplyTemplate overriding only applies to ControlTemplates or is my scenario included as well? Should I provide ControlTemplate instead of DataTemplate? Is the body of the method I have provided correct? </p>
  
  <p>Please help. Thanks. </p>