在 WPF 中的 MVVM 中的 Viewmodel 之间传递值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14933800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing values between Viewmodel in MVVM in WPF
提问by Roshil K
I am developing WPF Application using MVVM light tool kit.I have a datagrid in my Mainwindow.I have created another window named "openfile" and their viewmodels.Main Window viewmodel class contain public property of type ObservableCollection MyList which is bound to the Datagrid.Can I able to fill this property from the openfile Viewmodel and and automatically bind to Datagrid? or can I able to pass a varaible to MainViewmodel and make a Call to a public function in the MainViewmodel from the OpenfileViewmodel?
我正在使用 MVVM 灯光工具包开发 WPF 应用程序。我的主窗口中有一个数据网格。我创建了另一个名为“openfile”的窗口,它们的视图模型。主窗口视图模型类包含绑定到数据网格的 ObservableCollection MyList 类型的公共属性。我可以从 openfile Viewmodel 填充这个属性并自动绑定到 Datagrid 吗?或者我能否将一个变量传递给 MainViewmodel 并从 OpenfileViewmodel 调用 MainViewmodel 中的公共函数?
This is How I am calling MyPage From Menu bar.
这就是我从菜单栏中调用 MyPage 的方式。
private void NotificationMessageReceived(NotificationMessage msg)
{
switch (msg.Notification)
{
case Messages.MainVM_Notofication_ShowNewbWindow:
new NewView().ShowDialog();
break;
case Messages.MainVM_Notofication_ShowExistingWindow:
new OpenExisitingView().ShowDialog();
break;
case Messages.MainVM_Notofication_ShowotherWindow:
newView().ShowDialog();
break;
}
}
Thanks in Advance. Roshil K
提前致谢。罗希尔K
采纳答案by Roshil K
After a bit research I got the Current instance of my Mainviewmodel by the following Code.
经过一番研究,我通过以下代码获得了 Mainviewmodel 的 Current 实例。
MainViewModel mainViewModelInstaince = ServiceLocator.Current.GetInstance<MainViewModel>();
then I got all the methods and properties..and bound the datas from another viewmodel.
然后我得到了所有的方法和属性......并从另一个视图模型绑定了数据。
thanks to all..
谢谢大家..
回答by Mukesh Rawat
You can create a class which can be your "Mediator Service" and it will sit between your ViewModels. You can register your mediator service and add events which you can raise from one VM and handle in another. It can be like:
您可以创建一个可以作为“中介服务”的类,它将位于您的 ViewModel 之间。您可以注册您的中介服务并添加可以从一个 VM 引发并在另一个 VM 中处理的事件。它可以是这样的:
public class MediatorService: IMediatorService
{
public dynamic Data { get; set;}
public event EventHandler<YourCustomEventArgs> Callback = delegate { }
}
public class XYZVM(IMediatorService mediatorService)
{
// set your Data here and handle Callback event here and refresh your grid.
// you can get anything from your "YourCustomEventArgs" which you will set from ABCVM
}
public class ABCVM(IMediatorService mediatorService)
{
// get your data here and raise callback here and handle that in XYZVM
}
Hope this help you..
希望这对你有帮助..
回答by Andrey Gordeev
The easiest way is to pass MainWindowViewModel's instance to OpenFileViewModel:
最简单的方法是将MainWindowViewModel的实例传递给OpenFileViewModel:
public class OpenFileViewModel
{
private MainWindowViewModel _parent;
public OpenFileViewModel(MainWindowViewModel parent)
{
_parent = parent;
}
}
After that you can call/access any public method/property in MainWindowViewModel:
之后,您可以调用/访问任何公共方法/属性MainWindowViewModel:
foreach (var item in _parent.myList)
{
...
}

