WPF/MVVM:在同一DockPanel中打开不同的UserControl [英] WPF/MVVM: Opening different UserControls in same DockPanel

查看:385
本文介绍了WPF/MVVM:在同一DockPanel中打开不同的UserControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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?

推荐答案

要遵循的一般模式是:

  1. 在用户界面的位置添加ContentPresenter,该位置将托管可切换的内容(在您的情况下,DockPanel的右侧面板).
  2. 每个可切换视图的视觉表示将由用户控件表示.
  3. 每个可切换视图的数据将由viewModel表示.
  4. ContentPresenterContent属性绑定到视图模型中的属性,该属性是表示视图E.G.的viewModel类型. BoundContent.
  5. 单击链接时,您的viewModel应该对此做出反应,方法是更改​​bound属性并引发PropertyChanged事件以通知您的视图.
  6. 对于每个可以切换的视图",您将需要一个DataTemplate,用于将每个用户控件映射到每个viewModel.
  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 博客包含两个示例,它们演示了上述内容:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆