This problem is usually solved with the help of regions and RegionManager. In the main ViewModel window, a set of Regions is created and added to the RegionManager. ViewModels can then be enabled and added to the Region.Views collection.
In XAML, a region is typically entered using the ItemsSource property of the ItemsControl element associated with the region property of the main ViewModel.
So, on the main ViewModel screen, you will have something like this:
public class TestScreenViewModel { public const string MainRegionKey = "TestScreenViewModel.MainRegion"; public TestScreenViewModel(IUnityContainer container, IRegionManager regionManager) { this.MainRegion = new Region(); regionManager.Regions.Add(MainRegionKey, this.MainRegion); } public Region MainRegion { get; set; } }
This will be normally enabled in your IModule.
#region IModule Members public void Initialize() { RegisterViewsAndServices(); var vm = Container.Resolve<SelectorViewModel>(); var mainScreen = Container.Resolve<TestScreenViewModel>(); mainScreen.MainRegion.Add(vm); var mainView = ContentManager.AddContentView("Test harness", mainScreen); } #endregion
And the XAML representation of your template, similar to
<DataTemplate DataType="{x:Type TestModule:TestScreenViewModel}"> <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto"> <StackPanel> <ItemsControl ItemsSource="{Binding Path=MainRegion.Views}" /> </StackPanel> </ScrollViewer> </DataTemplate>
source share