I want to check the value of the ViewModel when my UserControl is going to unload. I want to do it using getting the variable value directly from the DataContext, the problem is that in the Unloaded event, the DataContext is equals to null. Is there a way to get the value from my ViewModel?
I'm trying somethin like this:
public MyView()
{
InitializeComponent();
Unloaded += (a, b) =>
{
var dc = DataContext as MyViewModel;
if (dc.IsChanged == true)
Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("ARE YOU SHURE YOU WANT TO EXIT WITHOUT SAVING CHANGES?", "WARNING", MessageBoxButton.OKCancel, MessageBoxImage.Warning)), System.Windows.Threading.DispatcherPriority.Normal);
};
}