WPF MVVM:怎样的ViewModels互相沟通? [英] WPF MVVM: How do ViewModels communicate with each other?

查看:423
本文介绍了WPF MVVM:怎样的ViewModels互相沟通?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个观点上它具有两个子视图和一个ViewModel被分配到每个视图:

I have a View which has 2 sub views on it and a ViewModel is assigned to each view:

ViewA - ViewModelA
{ 
   ViewB - ViewModelB
   ViewC - ViewModelC
}

ViewB有一个文本框和ViewC有一个下拉框,这两者我需要ViewModelA访问。没有GUI控件本身,而是绑定值即文本框。文本和组合框的.SelectedItem。目前,我只是有ViewModelB和ViewModelC作为ViewModelA属性,但感觉错了。

ViewB has a text box and ViewC has a combobox, both of which i need access from ViewModelA. Not the GUI control itself, but the bound value i.e. .Text of the textbox and .SelectedItem of the ComboBox. Currently i just have ViewModelB and ViewModelC as properties on ViewModelA but it feels wrong.

什么是对视图模型相互没有打破MVVM模式通信的标准方式?我完全新的WPF / MVVM。

What's the standard way for view models to communicate with each other without breaking the MVVM pattern? I'm completely new to WPF/MVVM.

推荐答案

一个办法已经断开的ViewModels传达给对方是使用发布/订阅机制,如 PRISM 小号的 EventAggregator 。然而,在父/子视图模型的关系,我认为它的罚款父有过孩子视图模型直接了解和控制。

One way to have disconnected ViewModels communicate to each other is to use a publish / subscribe mechanism such as PRISMs EventAggregator. However, in a parent / child ViewModel relationship, I think it's fine for the parent to have direct knowledge and control over the child ViewModel.

就个人而言,我不认为构成一个ViewModel出其他的ViewModels是一个不好的做法。我做这一切的时候。 我一般偏向于在我的ViewModels继承组成。

Personally, I don't think composing a ViewModel out of other ViewModels is a bad practice. I do it all the time. I generally favor composition over inheritance in my ViewModels.

这篇关于WPF MVVM:怎样的ViewModels互相沟通?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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