如何从其他视图模型调用主视图模型中的函数? [英] How to call functions in a main view model from other view models?
问题描述
我的程序由底层的一个 TreeView
和两个 contentPresenters
组成.mainWindow、TreeView
和每个 contentPresenter
都有自己的视图模型.
My program is composed of a TreeView
and two contentPresenters
at ground level. The mainWindow, TreeView
, and each contentPresenter
all have their own viewModels.
我想从 TreeViewViewModel
中调用 mainWindowViewModel
中的函数.
I would like to call a function in the mainWindowViewModel
from the TreeViewViewModel
.
我需要这样做是因为 mainWindowViewModel
控制了 contentPresenters
中显示的内容,我想手动更新显示.
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
}
}
我尝试使用以下方法从 TreeViewViewModel
访问 MainWindowVM
:
I have tried to get access to the MainWindowVM
from the TreeViewViewModel
by using:
public MainWindowViewModel ViewModel { get { return DataContext as MainWindowViewModel; } }
但这没有多大意义.因为 MWVM 不是 TreeViewViewModel
的 DataContext
.
But it doesn't make much sense. because the MWVM is not the DataContext
of the TreeViewViewModel
.
推荐答案
此中使用的
delegate
方法和链接的答案可用于任何父子关系和任一方向.这包括从子视图模型到父视图模型、Window
代码到子Window
代码后面的代码,甚至是不涉及任何 UI 的纯数据关系.您可以从 MSDN 上的委托(C# 编程指南) 页面.
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.
我今天早些时候刚刚回答了一个类似的问题.如果您查看 在视图模型之间传递参数 的帖子,您会看到答案涉及使用 delegate
对象.你可以简单地用你的方法替换这些 delegate
s(来自答案),它会以同样的方式工作.
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 delegate
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.
更新>>>
是的,抱歉,我完全忘记了您想改为调用方法……我今晚已经写了太多帖子.所以仍然使用其他帖子中的示例,只需在 ParameterViewModel_OnParameterChange
处理程序中调用您的方法:
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
}
将 delegate
视为返回父视图模型的路径...就像引发一个名为 ReadyForYouToCallMethodNow 的事件.
事实上,你甚至不需要有一个输入参数.你可以像这样定义你的 delegate
:
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();
}
由于您有多个子视图模型,您可以在它们都可以访问的另一个类中定义 delegate
.如果您还有其他问题,请告诉我.
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.
更新 2 >>>
再次阅读您的最后一条评论后,我刚刚想到了一种更简单的方法,可能实现您想要的……至少,如果我理解正确的话.您可以直接从您的子视图Bind
到您的父视图模型.例如,这将允许您将子视图中的 Button.Command
属性Bind
到父视图模型中的 ICommand
属性:
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:
在TreeViewView
中:
<Button Content="Click Me" Command="{Binding DataContext.ParentCommand,
RelativeSource={RelativeSource AncestorType={x:Type MainWindow}}}" />
这当然假设有问题的父视图模型的实例被设置为 MainWindow
的 DataContext
.
This of course assumes that an instance of the parent view model in question is set as the DataContext
of the MainWindow
.
这篇关于如何从其他视图模型调用主视图模型中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!