I know this question has been asked many times but I will try to be specific as possible.
I am a beginner in WPF / MVVM and using MVVM Light Toolkit by Galasoft in my project.
I have a view containing a form where the user inputs some patient details. When they click the close (the X) button, I want to check if they have entered something and if so ask them if they want to save before closing with (Yes, No and Cancel) options. I did some research and found that many are suggesting the EventToCommand feature like so,
XAML
<Window
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<cmd:EventToCommand Command="{Binding OnClosingCommand}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
...
</Window>
View Model
public class MainViewModel : ViewModelBase
{
public RelayCommand<CancelEventArgs> OnClosingCommand { get; set; }
public MainViewModel()
{
this.OnClosingCommand =
new RelayCommand<CancelEventArgs>(this.OnClosingCommandExecuted);
}
private void OnClosingCommandExecuted(CancelEventArgs cancelEventArgs)
{
// logic to check if view model has updated since it is loaded
if (mustCancelClosing)
{
cancelEventArgs.Cancel = true;
}
}
}
The above example is taken from Confirmation when closing window with 'X' button with MVVM light
However, the creator of the MVVM Light Toolkit himself is saying this breaks the separation of concern that the MVVM pattern is trying to achieve since it is passing the event arguments belonging to the view (in this case the CancelEventArgs) to the view model. He said so in this article http://blog.galasoft.ch/posts/2014/01/using-the-eventargsconverter-in-mvvm-light-and-why-is-there-no-eventtocommand-in-the-windows-8-1-version/
So my question is, What is the proper way to handle such problem which doesn't break the MVVM pattern. Any point in the right direction would be greatly appreciated!