Caliburn.Micro how to access the actual view model used in the view

I want to access the actual view model currently in use from inside the view (behind the code). In bootstrapper, I have a viewmodel installed perrequest, so I can not use IoC.Get <.. ViewModel> (); (and I do not want to change this behavior).

Basically, I'm looking for the equivalent of GetView from the screen, but then vice versa.

+4
source share
2 answers

The DataContext will provide you with the current ViewModel, which is used as the DataContext view.

// Get you the object of ViewModel.
var viewModelInstance = DataContext;

// Or typecast to exact instance what you intend to use.
MyViewModel vm = DataContext as MyViewModel;
+6
source

Keep in mind that it DataContextwill have the value assigned to it after loading View. For example, you can access it in a loaded event View.

0
source

All Articles