I followed Jason Dollinger's MVVM sample from Lab49 to learn the basics of using Unity with an MVVM WPF application. I constructed a simple sample following his basic architecture, using property injection and the Dependency attribute to inject viewmodels into the views. My sample has a main window with a child user control created in the window's XAML. The child control (and the main window, too) has a property for assigning the viewmodel:
[Dependency]
public IChildViewModel VM
{
set { this.DataContext = value;}
}
I wire everything up in app.xaml.cs:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
IUnityContainer container = new UnityContainer();
container.RegisterType<IWindowViewModel, Window1ViewModel>();
container.RegisterType<IChildViewModel, UserControl1ViewModel>();
Window1 window = container.Resolve<Window1>();
window.Show();
}
The main window is getting its viewmodel injected, but the child control is not. Is there any direct way of getting the resolution to propagate down into child controls? What kind of architectural changes would I need to make to do so? I'm not wedded to Unity at this point, so I can change to another container if this kind of behavior is supported.