I want to access the actual viewmodel that is currently used from within the view (code-behind). In the bootstrapper I have the viewmodel set to perrequest so I cannot use IoC.Get<..ViewModel>(); (nor do I want to change this behavior).
Basically, I'm looking for the equivalent of the GetView from the Screen, but then the other way around.
DataContext will give you the current ViewModel which is applied as DataContext of 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;
Bear in mind that the DataContext
will have a value assigned to it once the View
has been loaded. For example you can access to it in the loaded event of the View
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With