Is there a way to reuse a WPF View using it with Caliburn.Micro?
In example I've got a DetailViewBase.xaml with a SaveAndClose and Cancel button. Now I want to use the DetailViewModelBase and the XAML in PersonView(Model).
I hope this is question is clear.
You can bind the ViewModel to the View explicitly using code like this:
If my assumption is correct, I think what you mean is that you want to 'compose' a more complex view from several simpler viewmodels. You can inherit from viewmodels, but obviously a viewmodel can only have one active view at a time.
However, using bindings you can compose multiple viewmodels and have them all render their respective views together to make a more complex UI
e.g.
A VM with save/cancel buttons
The corresponding view:
Another view which composes itself and the
ActionsViewModel
The View:
When you use the convention binding (or use the attached properties) to bind a
ContentControl
CM will automatically bind the VM to theDataContext
and wire up the view. This way you can compose multiple VMs into a single more complex piece of functionality.Additionally, because of the action message bubbling - if you were to remove the 'Ok' and 'Cancel' implementation on the
ActionsViewModel
and put them in theComposedViewModel
implementation, the action message will bubble up to theComposedViewModel
instance and fire the methods on there insteade.g.
EDIT:
Sorry I forgot about this - convention based bindings won't allow messages to bubble, but you can just use the
Message.Attach
attached property for this to work: