GOAL:
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.
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 "navigate" away from the current view if the viewModle "HasErrors" 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.
What makes this hard: 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 (As outlined here).
Question:
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.
What I tried:
FIRST- I tried to have different command parameters for the different commands. ie:
<DataGrid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="LostFocus">
<cmd:EventToCommand Command="{Binding DataContext.PreNavigateValidateCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
CommandParameter="DataGridLostFocus"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
and for the controls in the datagrid
<DataGrid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="LostFocus">
<cmd:EventToCommand Command="{Binding DataContext.LostFocusValidateCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
CommandParameter="ControlostFocus"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
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.
SECOND- You will notice the different command properties/names. Even binding the commands to different command objects did not solve this problem. Both commands will be called, in the same order described above.
THIRD 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 "parent" objects.
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.
I would appreciate any help in isolating the lost focus event of the datagrid as whole that still follows MVVM standards.
Thanks