如何调用函数在其他视图模型的主视图模型? [英] How to call functions in a main view model from other view models?

查看:135
本文介绍了如何调用函数在其他视图模型的主视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序是由 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 >>>

再次阅读您的最后的评论之后,我只是认为的一个更简单的方法可能的实现你想要的......至少,如果我理解正确。它可能为你绑定直接从您的孩子的意见,以父视图模型。例如,这将允许您绑定 A Button.Command 在子视图属性设置为一个<$ C $在父视图模型C>的ICommand 属性:

TreeViewView

 &LT;按钮内容=点击我命令={结合DataContext.ParentCommand,
的RelativeSource = {的RelativeSource AncestorType = {X:类型主窗口}}}/&GT;

这当然假设问题父视图模型的实例设置为的DataContext 主窗口

My program is composed of a TreeView and two contentPresenters at ground level. The mainWindow, TreeView, and each contentPresenter all have their own viewModels.

I would like to call a function in the mainWindowViewModel from the TreeViewViewModel.

I need to do this because the mainWindowViewModel controls what is displayed in the contentPresenters, 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 the TreeViewViewModel 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 the TreeViewViewModel.

解决方案

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, a Window code behind to the code behind of a child Window, or even pure data relationships without any UI involved. You can find out more about using delegate 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 these delegates (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 called ReadyForYouToCallMethodNow. In fact, you don't even need to have an input parameter. You could define your delegate 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 to Bind a Button.Command property in a child view to an ICommand 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 the MainWindow.

这篇关于如何调用函数在其他视图模型的主视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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