I want to get a reference from a user control view model to the window that holds the user control. How can I do this?
If I can get a reference to the view from view model then I can use:
Window parentWindow = Window.GetWindow(userControlReference);
So my questions are:
- What's the best way to get the reference from user control view model to the window that holds the user control?
- If I want to use the above code, what is the best way to get a reference to view from view model in Caliburn Micro?
A viewmodel will generally inherit from IViewAware
assuming that it's an IScreen
implementation
You can just cast a Screen to IViewAware
and use the GetView()
method to get a reference to the view. I think you can implement IViewAware
on your own viewmodels and Caliburn will automatically raise the ViewAttached
event so you can keep a reference to the view but I might have a look at the docs
Bottom line: check out the IViewAware interface
Edit:
From the docs...
IViewAware – Implemented by classes which need to be made aware of the view that they are bound to. It has an AttachView method which is called by the framework when it binds the view to the instance. It has a GetView method which the framework calls before creating a view for the instance. This enables caching of complex views or even complex view resolution logic. Finally, it has an event which should be raised when a view is attached to the instance called ViewAttached.