简体   繁体   English

如何使用WPF MVVM在父窗口和子用户控件之间进行通信

[英]How to communicate between Parent Window and Child User Control using WPF MVVM

I am relatively new to WPF MVVM. 我对WPF MVVM比较陌生。 I am currently working on a small project to understand the details around the concepts of WPF MVVM and am facing a small issue that I cannot get my head around. 我目前正在做一个小项目,以了解有关WPF MVVM概念的详细信息,并且面临着一个小问题,无法解决。

I have a main window with a <ContentControl Content="{Binding Path=CurrentViewModel}" /> with the CurrentViewModel property being set on the main window ViewModel class. 我有一个带有<ContentControl Content="{Binding Path=CurrentViewModel}" />的主窗口,在主窗口ViewModel类上设置了CurrentViewModel属性。 The main window has three button controls for listing, editing and removing an entity. 主窗口具有三个按钮控件,用于列出,编辑和删除实体。 The ContentControl loads a UserControl (ListEntities) which lists all the entities from some data store. ContentControl加载一个UserControl(ListEntities),该控件列出某个数据存储中的所有实体。 The UserControl xaml has its own ViewModel class which is being set to the ContentControl content property and the view gets loaded using DataTemplates. UserControl xaml具有自己的ViewModel类,该类已设置为ContentControl内容属性,并且使用DataTemplates加载了视图。

This is all working fine. 一切正常。 My requirement is that when a user will select a row in the ListEntities UserControl GridView, the Edit and Remove buttons has to be enabled(using the CanExecute method of the ICommand infrastructure). 我的要求是,当用户在ListEntities UserControl GridView中选择一行时,必须启用“编辑”和“删除”按钮(使用ICommand基础结构的CanExecute方法)。 The issue is that I can set a SelectedEntity property on the UserControl ViewModel class based on SelectedItem property binding, but how do I get the selected item to be available to the main window view model class so as to enable the edit/remove buttons(how from the Parent View would I get to know that an item in the Child View has been selected using the MVVM pattern). 问题是我可以基于SelectedItem属性绑定在UserControl ViewModel类上设置SelectedEntity属性,但是如何使所选项目可用于主窗口视图模型类,从而启用编辑/删除按钮(如何我将从父视图中了解到已使用MVVM模式在子视图中选择了一个项目)。 So in a more generic way, I am trying to understand the most standard procedure to enable communication between the independent ViewModels of the Parent window and child User Control based on some event in either the Parent or the Child view. 因此,以一种更通用的方式,我试图理解最标准的过程,以基于“父”或“子”视图中的某些事件来启用“父”窗口的独立ViewModel和子“用户控件”之间的通信。

One a approach that I like to communicate between ViewModels is Publish and Subscribe . 我喜欢在ViewModels之间通信的一种方法是Publish and Subscribe Most of the MVVM-Frameworks provide something like an EventAggregator or a Messenger 大多数MVVM-Frameworks提供诸如EventAggregatorMessenger

The pattern is that you pass in the Messenger via DI into the ViewModel (sometimes also useful for views in a decoupled way) and on one side you send the notification on the other side you subscribe. 模式是,您通过DIMessenger传递到ViewModel (有时以分离的方式对视图也很有用),并且在一侧您订阅时发送通知。 Pub-Sub. Pub-Sub。

The also implement nice things like passing paylod, typed messages and callbacks to inform the sender back. 还实现了一些不错的功能,例如传递工资,输入的消息和回调以通知发件人。 You should definitely investigate further on this topic. 您绝对应该对此主题进行进一步调查。

Because it always starts with Parent Child and it will get fancy soon. 因为它总是以“亲子”开头,所以很快就会流行起来。 Open Dialogs for loading files, etc ... So for my point of view PubSub would be the way to go, to get the best decoupling here. 打开对话框以加载文件等。因此,就我个人的观点而言,PubSub是最好的去耦方法。 But handle with care too much can also end up confusing ... 但是小心轻放也可能导致混淆...

Prism 棱镜

Event Aggregator https://msdn.microsoft.com/en-us/library/ff921122(v=pandp.20).aspx 事件汇总器https://msdn.microsoft.com/zh-cn/library/ff921122(v=pandp.20).aspx

MVVM Light MVVM灯

http://dotnetpattern.com/mvvm-light-messenger ( http://www.mvvmlight.net/help/WP8/html/9fb9c53a-943a-11d7-9517-c550440c3664.htm ). http://dotnetpattern.com/mvvm-light-messengerhttp://www.mvvmlight.net/help/WP8/html/9fb9c53a-943a-11d7-9517-c550440c3664.htm )。

Reactive UI 反应式使用者介面

https://reactiveui.net/docs/handbook/message-bus/ https://reactiveui.net/docs/handbook/message-bus/

Caliburn Micro EventAggregator https://caliburnmicro.com/documentation/event-aggregator Caliburn Micro EventAggregator https://caliburnmicro.com/documentation/event-aggregator

Hope this helps (the links are just randomly sorted and not ordered by preference, so it's up to you =) ...) 希望这会有所帮助(链接只是随机排序,而不是按首选项排序,因此取决于您=)...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM