如何调用函数在其他视图模型的主视图模型? [英] How to call functions in a main view model from other view models?
问题描述
我的程序是由 TreeView控件
和两个内容presenters
在地面上的。在的主窗口, TreeView控件
,每个内容presenter
都有自己的ViewModels。
我想调用的函数 mainWindowViewModel
从 TreeViewViewModel
。
我需要这样做,因为 mainWindowViewModel
控制什么是显示在内容presenters
,和我想手动更新显示。
我猜我会做这样的事情...
TreeViewViewModel
:
公共类TreeViewViewModel
{
//我是否需要申报MainWindowVM? 公共TreeViewViewModel(){...} 私人无效功能()
{
//命令影响显示器 //手动调用函数MainWindowVM刷新查看
}
}
我试图访问该 MainWindowVM
从 TreeViewViewModel
使用:
公共MainWindowViewModel视图模型{{返回的DataContext作为MainWindowViewModel; }}
但它并没有多大意义。因为MWVM不是的DataContext
的 TreeViewViewModel
。
在这个使用的
委托
方法和链接的答案可以在任何亲子关系,并在两个方向使用。从子视图模型父视图模型包括一个窗口
code后面的code后面的子窗口
,或不涉及任何用户界面甚至是纯粹的数据关系。你可以找到更多关于使用委托
从的代表(C#编程指南)MSDN上页。
块引用>我刚才已经回答这个今天早些时候类似的问题。如果你看一看的<一个href=\"http://stackoverflow.com/questions/19513555/passing-parameters-between-viewmodels/19514713#19514713\">Passing之间的ViewModels 参数后,你会看到,答案涉及到使用
委托
的对象。你可以简单地用你的方法(S)代替这些委托
S(从答案),它会以同样的方式工作。请让我知道如果你有任何问题。
更新>>>
是的,对不起,我完全忘了,你想,而不是调用方法......我一直在今晚的工作太多的文章。所以仍然使用从其他职位的例子,只是叫你在
ParameterViewModel_OnParameterChange
处理方法:公共无效ParameterViewModel_OnParameterChange(字符串参数)
{
//调用你的方法在这里
}想想
委托的
作为是你的路径返回父视图模型......这就像养称为事件ReadyForYouToCallMethodNow。
事实上,你甚至不需要有一个输入参数。你可以定义你的委托
是这样的:公共委托无效ReadyForUpdate();公共ReadyForUpdate OnReadyForUpdate {搞定;组; }
然后是在父视图模型(在另一个例子连接处理程序等之后):
公共无效ChildViewModel_OnReadyForUpdate()
{
//调用你的方法在这里
UpdateDisplay();
}当你有多个子视图模型,你可以定义
代表
的另一个类,它们都可以访问。让我知道如果你有任何问题。更新2 >>>
再次阅读您的最后的评论之后,我只是认为的一个更简单的方法可能的实现你想要的......至少,如果我理解正确。它可能为你
绑定
直接从您的孩子的意见,以父视图模型。例如,这将允许您绑定
AButton.Command
在子视图属性设置为一个<$ C $在父视图模型C>的ICommand 属性:在
TreeViewView
:&LT;按钮内容=点击我命令={结合DataContext.ParentCommand,
的RelativeSource = {的RelativeSource AncestorType = {X:类型主窗口}}}/&GT;这当然假设问题父视图模型的实例设置为
的DataContext
的主窗口
。My program is composed of a
TreeView
and twocontentPresenters
at ground level. The mainWindow,TreeView
, and eachcontentPresenter
all have their own viewModels.I would like to call a function in the
mainWindowViewModel
from theTreeViewViewModel
.I need to do this because the
mainWindowViewModel
controls what is displayed in thecontentPresenters
, and I would like to manually update the display.I'm guessing I would do something like this...
TreeViewViewModel
:public class TreeViewViewModel { //Do I need to declare the MainWindowVM? public TreeViewViewModel() { ... } private void function() { //Command that affects display //Manually call function in MainWindowVM to refresh View } }
I have tried to get access to the
MainWindowVM
from theTreeViewViewModel
by using:public MainWindowViewModel ViewModel { get { return DataContext as MainWindowViewModel; } }
But it doesn't make much sense. because the MWVM is not the
DataContext
of theTreeViewViewModel
.解决方案The
delegate
method used in this and the linked answer can be used in any parent-child relationship and in either direction. That includes from a child view model to a parent view model, aWindow
code behind to the code behind of a childWindow
, or even pure data relationships without any UI involved. You can find out more about usingdelegate
objects from the Delegates (C# Programming Guide) page on MSDN.I just answered a similar question to this earlier today. If you take a look at the Passing parameters between viewmodels post, you'll see that the answer involves using
delegate
objects. You can simply replace thesedelegate
s (from the answer) with your method(s) and it will work in the same way.Please let me know if you have any questions.
UPDATE >>>
Yes, sorry I completely forgot you wanted to call methods instead... I've been working on too many posts tonight. So still using the example from the other post, just call your method in the
ParameterViewModel_OnParameterChange
handler:public void ParameterViewModel_OnParameterChange(string parameter) { // Call your method here }
Think of the
delegate
as being your path back to the parent view model... it's like raising an event calledReadyForYouToCallMethodNow.
In fact, you don't even need to have an input parameter. You could define yourdelegate
like this:public delegate void ReadyForUpdate(); public ReadyForUpdate OnReadyForUpdate { get; set; }
Then in the parent view model (after attaching the handler like in the other example):
public void ChildViewModel_OnReadyForUpdate() { // Call your method here UpdateDisplay(); }
As you have multiple child view models, you could define the
delegate
in another class that they both have access to. Let me know if you have any more questions.
UPDATE 2 >>>
After reading your last comment again, I've just thought of a much simpler method that might achieve what you want... at least, if I understand you correctly. It is possible for you to
Bind
directly from your child views to your parent view model. For instance, this would allow you toBind
aButton.Command
property in a child view to anICommand
property in your parent view model:In
TreeViewView
:<Button Content="Click Me" Command="{Binding DataContext.ParentCommand, RelativeSource={RelativeSource AncestorType={x:Type MainWindow}}}" />
This of course assumes that an instance of the parent view model in question is set as the
DataContext
of theMainWindow
.这篇关于如何调用函数在其他视图模型的主视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!