在WPF中的MVVM中的Viewmodel之间传递值

Ros*_*l K 6 silverlight wpf mvvm mvvm-light

我正在使用MVVM光工具包开发WPF应用程序.我在Mainwindow中有一个数据网格.我创建了另一个名为"openfile"的窗口,他们的viewmodels.Main Window viewmodel类包含ObservableCollection类型的公共属性,它绑定到Datagrid.我可以从openfile Viewmodel填充此属性并自动绑定到Datagrid吗?或者我可以将变量传递给MainViewmodel并从OpenfileViewmodel调用MainViewmodel中的公共函数吗?

这是我如何从菜单栏调用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;
            }
        }
Run Code Online (Sandbox Code Playgroud)

提前致谢.Roshil K.

Ros*_*l K 4

经过一番研究后,我通过以下代码获得了 Mainviewmodel 的当前实例。

MainViewModel mainViewModelInstaince = ServiceLocator.Current.GetInstance<MainViewModel>();
Run Code Online (Sandbox Code Playgroud)

然后我得到了所有的方法和属性......并绑定了另一个视图模型的数据。

谢谢大家..