WPF中的区域如何没有棱镜? [英] How to do Regions in WPF without Prism?

查看:70
本文介绍了WPF中的区域如何没有棱镜?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

特别是在MVVM Light工具包中吗?我还没有深入研究Prism代码,以了解它对WPF UI中的区域有何作用.就像在MVVM Light中使用消息传递和ViewModelLocator一样,您可以做类似的事情....可以吗?谁能举一些例子说明如何做到这一点?本质上,我想单击一个按钮,然后将两个不同的视图模型加载到UI中.也许更好的解释方法是类似Outlook的导航窗格功能.

Specifically in MVVM Light toolkit? I've not dove into the Prism code yet to see what it does in regard to regions in a WPF UI. Seems like with the messaging and the ViewModelLocator in MVVM Light you could do a similar thing....Can you? Can anyone give some examples on how you could do this? Essentially I'd like to click on a button and load two different view models into the UI. Perhaps a better way to explain is Outlook-like Navigation Pane functionality.

推荐答案

在WPF中,无需任何框架即可轻松完成此操作.

This can be done fairly easily in WPF, without any framework.

只需在应用程序(或应用程序中)中设置 DataTemplate Window/UserControl级别),将ViewModel映射到您希望为该ViewModel显示的视图.

Just setup a DataTemplate in your Application (or at the Window/UserControl level) that maps the ViewModel to the View you wish to display for that ViewModel.

然后,您可以只使用 ContentPresenter ,并将其内容绑定到ViewModel中的单个属性(可以是object类型).当您要将区域"设置为特定的View时,只需将属性设置为适当的ViewModel,WPF就会自动为您连接所有内容.

You can then just use a ContentPresenter, and bind it's contents to a single property (which can be of type object) within your ViewModel. When you want to set the "region" to a specific View, just set the property to the appropriate ViewModel, and WPF will automatically wire up everything for you.

这篇关于WPF中的区域如何没有棱镜?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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