6

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

5
  • 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. Commented Feb 18, 2013 at 10:14
  • How do you open OpenFile view? Commented Feb 18, 2013 at 10:19
  • I am opening OpenFile Window from Menu Bar. Commented Feb 18, 2013 at 10:22
  • I mean how do you do that by code. Show the code! Commented Feb 18, 2013 at 10:22
  • I used "Meessage " techmiques for MVVM Light tool kit to open openFile Menu. Commented Feb 18, 2013 at 10:27

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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)
{
    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.