title: Passing complex object to a Child Window
body: <p>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?</p>
  
  <p>Here's what I have: MainWindow.xaml (and in MainWindow.xaml.cs it assigns MainVM as its own DataContext)</p>
  
  <p>I would also like to have: ChildWindow.xaml and barebones ChildWindow.xaml.cs with ChildVM behind controls.</p>
  
  <p>So, now:</p>
  
  <ol>
  <li>How can I popup <code>ChildWindow</code> and pass some object <code>Data</code> to its
  <code>ChildVM</code>? </li>
  <li>Get the result (true/false) and result data (some complex
  object) back to <code>MainVM</code>? </li>
  <li>As a bonus, can changes in <code>Data</code> be observed
  by <code>MainVM</code> while they are being worked on by <code>ChildVM</code>?</li>
  </ol>
  
  <p>Here's what I tried - it doesn't solve everything, but is this even the right direction?</p>
  
  <ul>
  <li>For (2), I created a subclass of <code>Window</code>, called <code>DialogWindow</code>, which has 3 DependencyProperties: <code>Data</code> (for input data), <code>ResultData</code> (for output data) and <code>ResultValue</code> (for a bool result). </li>
  <li><code>ResultData</code> and <code>ResultValue</code> are both set by the <code>ChildVM</code> of <code>DialogWindow</code> using Binding, and when <code>ResultValue</code> is set, the <code>DialogWindow</code> closes.</li>
  <li><p>At the moment, the <code>ChildWindow</code> is launched (for all intents and purposes) from MainWindow.xaml.cs - kinda bad. I can then pass some input data, like so:</p>
  
  <p><code>ChildDialogWindow w = new ChildDialogWindow();</code></p>
  
  <p><code>w.Data = myDataObj;</code></p></li>
  </ul>
  
  <p>So, now I need to have a property <code>Data</code> on ChildVM, and set in ChildDialogWindow.xaml.cs. Again, making .xaml.cs thicker.</p>
  
  <p>I thought that maybe a better approach that avoids MainWindow.xaml.cs would be some kind of <code>DialogService</code> which is passed to <code>MainVM</code> as a dependency. But then, how can I pass values to the <code>ChildVM</code>?</p>