Let’s say I have a WPF application which makes use of the MVVM pattern. The application’s main window defines its data context in the XAML:
<Window.DataContext>
<vm:MainWindowViewModel/>
</Window.DataContext>
Is it possible to get a reference to the current instance of MainWindowViewModel in the XAML code-behind after InitializeComponent()? I know this is not recommended when using MVVM but I can’t figure out any other way for solving my problem.
sure:
Just cast your DataContext to the type of your viewmodel.