WPF/MVVM:在同一DockPanel中打开不同的UserControl [英] WPF/MVVM: Opening different UserControls in same DockPanel
问题描述
我正在使用DockPanel
创建我的布局的应用程序.在左侧" DockPanel
中,我设置了一些链接(标签),单击链接后,这些链接可用来打开不同的UserControls
.我希望在单击相应的链接后,UserControls
始终在右侧" DockPanel中打开.
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.
我将使用WPF(使用MVVM)中的哪种机制来完成此任务?有没有我可以查看的示例?
What mechanism in WPF (using MVVM) would I use to accomplish this? Are there any examples that I can view?
推荐答案
要遵循的一般模式是:
- 在用户界面的位置添加
ContentPresenter
,该位置将托管可切换的内容(在您的情况下,DockPanel
的右侧面板). - 每个可切换视图的视觉表示将由用户控件表示.
- 每个可切换视图的数据将由viewModel表示.
- 将
ContentPresenter
的Content
属性绑定到视图模型中的属性,该属性是表示视图E.G.的viewModel类型.BoundContent
. - 单击链接时,您的viewModel应该对此做出反应,方法是更改bound属性并引发
PropertyChanged
事件以通知您的视图. - 对于每个可以切换的视图",您将需要一个
DataTemplate
,用于将每个用户控件映射到每个viewModel.
- Add a
ContentPresenter
to the place in your user interface which will host the switch-able content (the right panel of theDockPanel
in 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
Content
property of theContentPresenter
to 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
PropertyChanged
event to notify your view. - For each 'view' that can be switched you will need a
DataTemplate
that maps each user control to each viewModel.
Rachel Lims 博客包含两个示例,它们演示了上述内容:
Rachel Lims blog contains a couple of examples which demonstrates the above:
- Switching views in WPF/MVVM
- Navigation with MVVM
这篇关于WPF/MVVM:在同一DockPanel中打开不同的UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!