Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing values between Viewmodel in MVVM in WPF

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?

This is How I am calling MyPage From Menu bar.

 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

like image 310
Roshil K Avatar asked Feb 18 '13 10:02

Roshil K


People also ask

How do I transfer data from one ViewModel to another?

To pass data from the Main View/Main ViewModel to the Detail ViewModel, assign the data to the ViewModelExtensions. Parameter attached property on the Detail View instance.

How do you communicate between two ViewModels?

There are two ways to pass information between View Models: Implement the ISupportParameter interface to pass parameters between View Models: Passing Data Between ViewModels (ISupportParameter). Use the Messenger service: Layer Communication. Messenger.

Can ViewModel contain other ViewModels?

It is perfectly right to have ViewModel contains ViewModel. Actually that is the recommended practice: To have one main ViewModel for the whole page, the main ViewModel could contain ViewModels for the sub views in the page.


2 Answers

After a bit research I got the Current instance of my Mainviewmodel by the following Code.

MainViewModel mainViewModelInstaince = ServiceLocator.Current.GetInstance<MainViewModel>();

then I got all the methods and properties..and bound the datas from another viewmodel.

thanks to all..

like image 162
Roshil K Avatar answered Sep 21 '22 13:09

Roshil K


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:

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..

like image 35
Mukesh Rawat Avatar answered Sep 20 '22 13:09

Mukesh Rawat