如何从主窗口中的命令在用户控件视图模型处理 [英] How a command from main window be handled in a usercontrol view model

查看:176
本文介绍了如何从主窗口中的命令在用户控件视图模型处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有几个用户控件的窗口。这些用户控制需要传达给对方。我的做法是如下

In my application I have several user controls in a window. These user controls need to communicate to each other. My approach is as below


  1. 的用户控制火的命令是由用户调节B进行处理。

  2. 主窗口临危指挥和火命令到B

  3. 用户对比B处理命令。

我的问题是主窗口如何激发它可以在用户控件B的视图模型?

My problem is how the main window can fire a command which can be handled in the VIEW MODEL of user control B?

更新:

解决:从主窗口通信到B和一个全局的RoutedCommand通信从A到主窗口的一个gloabl静态RelayCommand。感谢您的答复。

SOLVED: One gloabl static RelayCommand for communication from the MainWindow to B and one Global RoutedCommand for communication from A to Main Window. Thank you for the responses.

推荐答案

现在的问题是不是标记 MVVM (还),但我假设,因为你提到一个视图模型。所以,首先,让我们同意的ViewModels处理命令,而不是控制。如果控制要响应命令执行,露出一个合适的 CommandExecuted 事件控制(视图)可以附加一个处理程序。

The question is not tagged mvvm (yet), but I am assuming it since you mention a ViewModel. So to begin with, let's agree that ViewModels handle commands, not controls. If a control want to respond to a command being executed, expose a suitable CommandExecuted event that the control (View) can attach a handler to.

然而有另外一个未知的,我不能盲目地假设:

There is however another unknown that I cannot blindly assume:

你想当然地认为用户控制A和B同时存在吗?如果是的话,那么很简单,技术含量低的解决方案将是在一定程度上(也许是视图模型为窗口本身?)和泡沫,以保持双方的ViewModels引用命令到那里,那里就会发生派遣视图模型B点。如果不是(在这种情况下,控制B可能不存在),什么是所期望的行为?

Do you take it for granted that user controls A and B exist at the same time? If yes, then a very simple and low-tech solution would be to maintain references to both ViewModels at some level (perhaps the ViewModel for the Window itself?) and "bubble" the command up to there, where the dispatch to ViewModel B will occur. If not (in which case control B might not exist), what is the desired behavior?

随着到目前为止,您所提供的信息,我会用你MVVM框架的事件聚合/ Mediator模式实施建议。例如,棱镜有 EventAggregator ; MVVM光有信使;等等。

With the information you have given so far, I 'd suggest using the Event Aggregator/Mediator pattern implementation of your MVVM framework. For example, Prism has EventAggregator; MVVM Light has Messenger; etc.

这篇关于如何从主窗口中的命令在用户控件视图模型处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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