title: Event handlers in XAML or Code Behind
body: <p>I am new to Silverlight and XAML.  While trying to learn the syntax and best practices I continue to come across a discrepancy (or at least to me it seems that way) in the way some implement event handlers.</p>
  <p>In an <a href="http://msdn.microsoft.com/en-us/library/cc189066%28VS.95%29.aspx" rel="noreferrer">example</a> from MSDN I see the following code used:</p>
  <pre><code>&lt;UserControl x:Class=&quot;DragAndDropSimple.Page&quot;
      xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; 
      xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot; 
      Width=&quot;400&quot; Height=&quot;300&quot;&gt;
    &lt;Canvas x:Name=&quot;rootCanvas&quot;
    Width=&quot;640&quot;
    Height=&quot;480&quot;
    Background=&quot;Gray&quot;
    &gt;
      &lt;!-- You can drag this rectangle around the canvas. --&gt;
      &lt;Rectangle
      MouseLeftButtonDown=&quot;Handle_MouseDown&quot;
      MouseMove=&quot;Handle_MouseMove&quot;
      MouseLeftButtonUp=&quot;Handle_MouseUp&quot;
      Canvas.Left=&quot;30&quot; Canvas.Top=&quot;30&quot; Fill=&quot;Red&quot;
      Width=&quot;50&quot; Height=&quot;50&quot; /&gt;
    &lt;/Canvas&gt;
  
  &lt;/UserControl&gt;
  </code></pre>
  <p>Where the mouse handlers are set, however, in other <a href="https://stackoverflow.com/questions/741956/wpf-pan-zoom-image/927134#927134">code</a> I have seen this method used in the code behind:</p>
  <pre><code> public Window1()
          {
              InitializeComponent();
  
              TransformGroup group = new TransformGroup();
  
              ScaleTransform xform = new ScaleTransform();
              group.Children.Add(xform);
  
              TranslateTransform tt = new TranslateTransform();
              group.Children.Add(tt);
  
              image.RenderTransform = group;
  
              image.MouseWheel += image_MouseWheel;
              image.MouseLeftButtonDown += image_MouseLeftButtonDown;
              image.MouseLeftButtonUp += image_MouseLeftButtonUp;
              image.MouseMove += image_MouseMove;
          }
  </code></pre>
  <p>I would assume the example on MSDN is the recommended way, however, I tend to like the second method.</p>
  <p>Is there a best practice for this situation?</p>