6
votes

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

3
Would you be able to provide some example code for the scenario you are describing? If MyList is already bound to the Datagrid, then any updates to the list should automatically update your grid. if MyList is a public property, and the open file VM has a reference to your main VM then you should be able to populate it directly. - Dutts
How do you open OpenFile view? - Andrey Gordeev
I am opening OpenFile Window from Menu Bar. - Roshil K
I mean how do you do that by code. Show the code! - Andrey Gordeev
I used "Meessage " techmiques for MVVM Light tool kit to open openFile Menu. - Roshil K

3 Answers

4
votes

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

2
votes

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

1
votes

The easiest way is to pass MainWindowViewModel's instance to 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:

foreach (var item in _parent.myList)
{
    ...
}