Here is a simpler way by handle the Window's
Closing
event that allows for canceling the event if required. ie: if there are data changes pending for example:
public partial class MainWindow : Window
{
public MainWindow()
{
Closing += OnClosing;
InitializeComponent();
}
private void OnClosing(object sender, CancelEventArgs e)
=> e.Cancel = ((IRequestClose)DataContext).IsCancelled();
}
public interface IRequestClose
{
bool IsCancelled();
}
Then apply the interface to the ViewModel and implement the method.