WPF/MVVM: Opening different UserControls in same DockPanel

半世苍凉 提交于 2019-11-28 11:08:16

问题


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:

  1. Add a ContentPresenter to the place in your user interface which will host the switch-able content (the right panel of the DockPanel in your case).
  2. The visual representation of each switchable view will be represented by a user control.
  3. The data of each switch-able view will be represented by a viewModel.
  4. Bind the Content property of the ContentPresenter to a property in your view model that is of a viewModel type that represents the view E.G. BoundContent.
  5. When the link is clicked, your viewModel should react to this by changing the bound property and raising the PropertyChanged event to notify your view.
  6. For each 'view' that can be switched you will need a DataTemplate that 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!