I am fairly new to mvvm and mvvm light, but I think I understand the general idea of it. What I don't understand is if I want to open a new window, but that window needs data from the caller what is the best practice to get that data to the new window? If I pass the data to the constructor then that means I need code in the code behind to pass it to the view model. I can't use messaging, because it isn't basic data.
Asked
Active
Viewed 8,443 times
8
-
Sorry to resurrect this from the ancient past, but what to you mean by " I can't use messaging, because it isn't basic data." ? I'm asking this because I'm learning MVVM and if I was in your situation I would use Messaging. – lightxx Jul 01 '15 at 11:42
-
I'm sorry it has been too long for me to remember – Ian Overton Jul 02 '15 at 21:05
1 Answers
7
One popular choice is to use a service class that will create a view/viewmodel and display the new view. Your view model constructor and/or method/property could receive the data from the caller and then the view would be bound to the viewmodel prior to displaying it on the screen.
here is a very very simple implementation of a DialogService:
public class DialogService : IDisposable
{
#region Member Variables
private static volatile DialogService instance;
private static object syncroot = new object();
#endregion
#region Ctr
private DialogService()
{
}
#endregion
#region Public Methods
public void ShowDialog(object _callerContentOne, object _callerContentTwo)
{
MyDialogViewModel viewmodel = new MyDialogViewModel(_callerContentOne, _callerContentTwo);
MyDialogView view = new MyDialogView();
view.DataContext = viewmodel;
view.ShowDialog();
}
#endregion
#region Private Methods
#endregion
#region Properties
public DialogService Instance
{
get
{
if (instance == null)
{
lock (syncroot)
{
if (instance == null)
{
instance = new DialogService();
}
}
}
return instance;
}
}
#endregion
}

Backlash
- 541
- 3
- 8
-
would you please show me a basic example? I'm not sure I follow you completely. – Ian Overton Jan 07 '13 at 15:21
-
geedubb that only works for silverlight. I am building a wpf application on visual studio 2010. – Ian Overton Jan 07 '13 at 16:02
-
Actually, that is not a Silverlight-only solution geedbubb posted. It may be tailored for Silverlight, but Silverlight and WPF are very very similar. The idea is to learn from the techniques implemented in that link and then tailor it for your own personal solution. – Backlash Jan 07 '13 at 16:23