Reputation: 2715
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
Upvotes: 6
Views: 5460
Reputation: 2097
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..
Upvotes: 2
Reputation: 2715
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..
Upvotes: 4
Reputation: 32559
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)
{
...
}
Upvotes: 1