I have a Parent and Child viewmodel:
public class ParentViewModel : Screen
{
public ChildViewModel Child { get; set; }
}
public class ChildViewModel : PropertyChangedBase, IChild { }
When the Parent VM is displayed using Conductor.ActivateItem()
, Caliburn.Micro does the usual labours of searching through the view model object graph and looking for views to display etc.
After all this is completed, I find that while ParentViewModel.Parent
is a reference to the Conductor, ChildViewModel.Parent
is null
.
Is this by design?
Many thanks in advance.
One workaround for this is for the parent VM to pass itself to the child VM somehow.
Either by setting the
Parent
property explicitly:Or, assuming you have some sort of IoC-based abstraction for initialising view model objects, you could have a convention of implementing
Configure()
methods that returnthis
:It should be possible to customise Caliburm.Micro's view model handling code to set the parent automatically, but I'm not clever enough with it yet to know how to do that, yet. :)
Yes this is by design, the method that sets the
Parent
property isEnsureItem
in theConductorBase
, therefore it's only conductors that will set it when the active item is changed.The
Parent
is therefore also only available in the activation life cycle of the child, e.g.OnInitialise
orOnActivate
etc.Is there any reason your
ParentViewModel
isn't aConductor
type and yourChildViewModel
a Screen?Also, depending on the
Parent
property may be introducing coupling in yourChildViewModel
.