What's the right approach to open a child window (for example, to modify a selected item on the main window) keeping MVVM in mind?
Here's what I have: MainWindow.xaml (and in MainWindow.xaml.cs it assigns MainVM as its own DataContext)
I would also like to have: ChildWindow.xaml and barebones ChildWindow.xaml.cs with ChildVM behind controls.
So, now:
- How can I popup
ChildWindowand pass some objectDatato itsChildVM? - Get the result (true/false) and result data (some complex
object) back to
MainVM? - As a bonus, can changes in
Databe observed byMainVMwhile they are being worked on byChildVM?
Here's what I tried - it doesn't solve everything, but is this even the right direction?
- For (2), I created a subclass of
Window, calledDialogWindow, which has 3 DependencyProperties:Data(for input data),ResultData(for output data) andResultValue(for a bool result). ResultDataandResultValueare both set by theChildVMofDialogWindowusing Binding, and whenResultValueis set, theDialogWindowcloses.At the moment, the
ChildWindowis launched (for all intents and purposes) from MainWindow.xaml.cs - kinda bad. I can then pass some input data, like so:ChildDialogWindow w = new ChildDialogWindow();w.Data = myDataObj;
So, now I need to have a property Data on ChildVM, and set in ChildDialogWindow.xaml.cs. Again, making .xaml.cs thicker.
I thought that maybe a better approach that avoids MainWindow.xaml.cs would be some kind of DialogService which is passed to MainVM as a dependency. But then, how can I pass values to the ChildVM?