title: WPF Custom Control - Button Event
body: <p>I start the WPF today, I'm trying to implement a custom control.</p>
  
  <p>My problem is that I can not select an element in the template.</p>
  
  <p>My code:</p>
  
  <p>[Generic.xaml]</p>
  
  <pre><code> &lt;Style x:Key="{x:Type local:ImageButton}" TargetType="{x:Type local:ImageButton}"  BasedOn="{StaticResource {x:Type Button}}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type local:ImageButton}"&gt;
                  &lt;StackPanel&gt;
                      // -&gt; My image
                      &lt;Image Source="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}"&gt;&lt;/Image&gt;
                  &lt;/StackPanel&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>[ImageButton.cs]</p>
  
  <pre><code>public class ImageButton : Button
  {
      public Image imageOff { get; set; }
      public Image imageOn { get; set; }
  
      static ImageButton()
      {
          DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
      }
  
      public ImageButton()
          : base()
      {
          this.MouseEnter += new MouseEventHandler(SetImageON);
      }
  
      public void SetImageON(object sender, MouseEventArgs e)
      {
         //Here i wanna change my image from StackPanel
      }
  }
  </code></pre>
  
  <p>Am I on the good way ? How can I change that image?</p>