问题
I am creating an application using the DockPanel for my layout. In the 'Left' DockPanel I have setup a few links (Labels) that I would like to use to open different UserControls when a link is clicked. I want the UserControls to always open in the 'Right' DockPanel when the appropriate link is clicked.
What mechanism in WPF (using MVVM) would I use to accomplish this? Are there any examples that I can view?
回答1:
The general pattern to follow is this:
- Add a
ContentPresenterto the place in your user interface which will host the switch-able content (the right panel of theDockPanelin your case). - The visual representation of each switchable view will be represented by a user control.
- The data of each switch-able view will be represented by a viewModel.
- Bind the
Contentproperty of theContentPresenterto a property in your view model that is of a viewModel type that represents the view E.G.BoundContent. - When the link is clicked, your viewModel should react to this by changing the bound property and raising the
PropertyChangedevent to notify your view. - For each 'view' that can be switched you will need a
DataTemplatethat maps each user control to each viewModel.
Rachel Lims blog contains a couple of examples which demonstrates the above:
- Switching views in WPF/MVVM
- Navigation with MVVM
来源:https://stackoverflow.com/questions/13863986/wpf-mvvm-opening-different-usercontrols-in-same-dockpanel