title: Why to avoid the codebehind in WPF MVVM pattern?
body: <p>At the article, <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx" rel="noreferrer">WPF Apps With The Model-View-ViewModel Design Pattern</a>, the author who is Josh Smith said:</p>
  
  <blockquote>
    <p>(1) In a well-designed MVVM architecture, the codebehind for most Views should be empty, or, at most, only contain code that manipulates the controls and resources contained within that view. (2) Sometimes it is also necessary to write code in a View's codebehind that interacts with a ViewModel object, such as hooking an event or calling a method that would otherwise be very difficult to invoke from the ViewModel itself.</p>
  </blockquote>
  
  <p><s>My question is ,at the (1), why the empty codebehind is regarded as a a well-designed MVVM.</s>(It sounds that the empty codebehind is always good.)</p>
  
  <p><strong>EDIT: My question is, as the following, why the approach like the <code>AttachedCommandBehavior</code> or the <code>InvokeCommandAction</code> is tried to avoid the codebehind coding.</strong></p>
  
  <p>Let me explain more detail.</p>
  
  <p>As far as the (1) is concerned, I would think like the following situation as from the <a href="http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/" rel="noreferrer">AttachedCommandBehavior</a>. As the Border doesn't implement the <code>ICommandSource</code> for the <code>MouseRightButtonDown</code>, you cannot commonly bind the event and the <code>ICommand</code>, but can do with the <a href="http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/" rel="noreferrer">AttachedCommandBehavior</a>.</p>
  
  <pre><code>&lt;!-- I modified some code from the AttachedCommandBehavior to show more simply --&gt;
  &lt;Border&gt;
      &lt;local:CommandBehaviorCollection.Behaviors&gt;
             &lt;local:BehaviorBinding Event="MouseRightButtonDown" 
                    Command="{Binding SomeCommand}" 
                    CommandParameter="A Command on MouseRightButtonDown"/&gt;
      &lt;/local:CommandBehaviorCollection.Behaviors&gt;
  &lt;/Border&gt;
  </code></pre>
  
  <p><strong>OR</strong></p>
  
  <p>We can do this with the <code>System.Windows.Interactivity.InvokeCommandAction</code>.</p>
  
  <pre><code>&lt;Border xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" &gt;
      &lt;i:Interaction.Triggers&gt;
          &lt;i:EventTrigger EventName="MouseRightButtonDown"&gt;
              &lt;i:InvokeCommandAction Command="{Binding SomeCommand}" 
                 CommandParameter="A Command on MouseRightButtonDown"/&gt;
          &lt;/i:EventTrigger&gt;
      &lt;/i:Interaction.Triggers&gt;
  &lt;/Border&gt;
  </code></pre>
  
  <p><strong>BUT,</strong></p>
  
  <p>We use the following XAML and its codebehind having the <code>Border_MouseRightButtonDown</code> method, which is linked to the (2) Josh Simth said above.</p>
  
  <pre><code>&lt;Border MouseRightButtonDown ="Border_MouseRightButtonDown"/&gt;
  </code></pre>
  
  <p>I think using the codebehind as above is not bad just because the difference between these is only where binding a command or add event handler is.</p>
  
  <p>What do you think about this?</p>