I am building a Silverlight app which comprises a TreeView of menu options in a lefthand column and a ContentView in a righthand column. The idea is that the SelectedItemChanged event of the TreeView will change the view in the content area.
What is the ‘purest MVVM’ way of achieving this?
My idea is to have a TreeMenuView and TreeMenuViewModel for managing the menu events, but after that I’m a bit lost. I could use an EventAggregator to send a message from the TreeMenuViewModel to a `ContentViewModel’ that would then set its current ContentView based on the message args- but surely that breaks MVVM, in the sense that a ViewModel shouldn’t know about UI constructs like a View?
Am I missing something simple here?
How does a ViewModel layer drive the View selection?
I would create a
ShellViewModelwhich had:ObservableCollection<ViewModelBase> AvailablePagesint SelectedPageIndexViewModelBase CurrentPage, which returnsAvailablePages[SelectedPageIndex]Your
ShellViewcan be anything you want. If you want to display yourAvailablePagesin aTreeView, then go ahead. Just remember to bindSelectedIndexto`SelectedPageIndexIn your case, I would create a
DockPanelwith aTreeViewon the Left bound toAvailablePages, and aContentControlon the right withContentControl.Contentbound toCurrentPageEdit
Here’s an example
Then use
DataTemplatesto define how the ContentControl containingCurrentPagewill look