MVVM-Light:将UserControl加载到窗口中 [英] MVVM-Light: Load UserControl into Window

查看:219
本文介绍了MVVM-Light:将UserControl加载到窗口中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MVVM-Light将UserControl加载到窗口中的最佳实践是什么? 在从Window的ViewModel中或直接在窗口中创建UserControl实例的地方?

What is the best practice to load an UserControl into an Window with MVVM-Light? Where create the UserControl instance, in the ViewModel from Window or direct in the window?

推荐答案

如果您担心最佳实践,则您的VM不应包含任何特定于UI的代码(如控件).例如,如果您在VM中具有控制视图中控件可见性的属性,则该属性应为bool类型而不是Visibility.该视图将使用bool属性来隐藏或显示控件.

Your VM should not contain any UI specific code like controls if you are worried about best practice. For example, if you have a property in the VM that controls the visibility of a control in your view, that property should be of type bool and not Visibility. The view will use the bool property to hide or show the control.

因此,您有一个视图(如窗口)与VM绑定数据. VM中的某些属性更改将导致窗口中的某些代码创建并显示用户控件.

So you have a view (like a window) which databinds against a VM. Some property changes in the VM which will cause some code in the window to create and show a user control.

这篇关于MVVM-Light:将UserControl加载到窗口中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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