title: Recognise the lost focus event of a DataGrid as a whole, not its controls lost focus
body: <h1><strong>GOAL:</strong></h1>
  <p>I have an WPF/MVVM (mvvm-light to be exact) application where I want to recognise/capture the lostfocus event of the datagid as a whole and bind it to an ICommand in the view model.</p>
  <p>The problem is that the lost focus event is fired every time a control within the datagrid loses focus as well as the datagrid itself losing focus.  In my applications I throw a warning (a MVVM type message box) on the datagrid lostfocus event/command if a user tries to &quot;navigate&quot; away from the current view if the viewModle &quot;HasErrors&quot; property is true.  The result is that even if the user moves between controls in the datagrid, the user gets this error/warning.  I only want it when the datagrid as a whole loses focus.</p>
  <p><strong>What makes this hard:</strong>
  Simply put, what makes this hard is using the MVVM.  Usually you could just check the FocusManager in the code behind lostfocus event to get the currently focussed element and see if its in the datagrid (<a href="http://code.msdn.microsoft.com/windowsdesktop/DataGrid-Lost-Focus-46bb4526" rel="nofollow noreferrer">As outlined here</a>).</p>
  <h2><strong>Question:</strong></h2>
  <p>Is there a MVVM standard solution to this problem?  I am not so blindly die hard MVVM to never have code behind, I guess I am just wondering if this is one of those times or is there some strategy.option I didn't think about, which is likely.</p>
  <h2><strong>What I tried:</strong></h2>
  <p><strong>FIRST</strong>-  I tried to have <strong>different command parameters</strong> for the different commands.  ie:</p>
  <pre><code>&lt;DataGrid&gt;
      &lt;i:Interaction.Triggers&gt;
          &lt;i:EventTrigger EventName=&quot;LostFocus&quot;&gt;
              &lt;cmd:EventToCommand Command=&quot;{Binding DataContext.PreNavigateValidateCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}&quot;
              CommandParameter=&quot;DataGridLostFocus&quot;/&gt;
          &lt;/i:EventTrigger&gt;
      &lt;/i:Interaction.Triggers&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  <p>and for the controls in the datagrid</p>
  <pre><code>&lt;DataGrid&gt;
      &lt;i:Interaction.Triggers&gt;
          &lt;i:EventTrigger EventName=&quot;LostFocus&quot;&gt;
              &lt;cmd:EventToCommand Command=&quot;{Binding DataContext.LostFocusValidateCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}&quot;
               CommandParameter=&quot;ControlostFocus&quot;/&gt;
          &lt;/i:EventTrigger&gt;
      &lt;/i:Interaction.Triggers&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  <p>notice the difference in the command parameters of DataGridLostFocus and ControlLostFocus.  But what happens is that these commands just happen twice, one for each command parameter with the control lostfocus happening first followed by the datadrid lostfocus.</p>
  <p><strong>SECOND</strong>- You will notice the <strong>different command properties/names</strong>.  Even binding the commands to different command objects did not solve this problem.  Both commands will be called, in the same order described above.</p>
  <p><strong>THIRD</strong> The datagrid is inside a grid, which is inside an expander, which is inside a usercontrol.  I tried moving the ICommand triggerbinding up the visual tree to these three elements.  The lost focus event gets fired the same way even when placed on any of these three &quot;parent&quot; objects.</p>
  <hr />
  <p>I am starting to think I eighter need to find another event that would work or totally rethink how I handle this error trigger for the viewmodels's HasErrors property.</p>
  <p>I would appreciate any help in isolating the lost focus event of the datagrid as whole that still follows MVVM standards.</p>
  <p>Thanks</p>