title: MVVM and View/ViewModel hierarchy
body: <p>I'm working on making my first game using C# and XAML for Windows 8. I'm still learning the core concepts and best practices, and MVVM has been a hurdle. I'll attempt to ask the question in two parts.</p>
  
  <p><strong>Background</strong></p>
  
  <p>The game I'm making is Sudoku. Sudoku has a board that contains a 9x9 grid of tiles. I have three models - <code>Game</code>, <code>Board</code>, and <code>Tile</code>. When a <code>Game</code> is created, it automatically creates a <code>Board</code>, and when the <code>Board</code> is created, it creates 81 (9x9) <code>Tiles</code>.</p>
  
  <p><strong>1. With a hierarchy of views, how are corresponding view models created?</strong></p>
  
  <p>To match the hierarchy of models, I would like to have a hierarchy of views (<code>GameView</code> contains a <code>BoardView</code> which contains 81 <code>TileViews</code>). In XAML, it's pretty easy to create this hierarchy of views with user controls, but I don't understand how the view models get created.</p>
  
  <p>In the examples I've seen, the data context of a user control is often set to the view model (using the <code>ViewModelLocator</code> as a source) which creates a fresh instance of the view model. This seems to work well if you have a flat view, but also seems like it gets messy when you have a hierarchy. Does the <code>GameView</code> create a <code>GameViewModel</code> and leave it up to its <code>BoardView</code> child to create a <code>BoardViewModel</code>? If so, how does the <code>GameViewModel</code> communicate with the <code>BoardViewModel</code>? Can the <code>BoardViewModel</code> communicate back up the hierarchy to the <code>GameViewModel</code>?</p>
  
  <p><strong>2. How does a view model get model data?</strong></p>
  
  <p>In iOS, I would start by using a service to fetch a <code>Game</code> model that was pre-populated with data. I would then create a <code>GameViewController</code> view controller (which was in charge of creating the view) and pass the <code>Game</code> to it. In MVVM, I see the value in having a view be in charge of creating its own view model (ideally using a <code>ViewModelLocator</code>), but I don't understand how that view model gets the model.</p>
  
  <p>In all of the examples I've found online, the view model uses some service to fetch its own data. But I haven't come across any example that accepts constructor params or params passed from a higher level of navigation. How is this done?</p>
  
  <p>I don't want to use an application resource or some other kind of singleton storage method for my model because, not that I do, but what if I wanted to display multiple puzzles on the screen at once? Each <code>GameView</code> should contain its own <code>Game</code>.</p>
  
  <p>Not only does the <code>GameViewModel</code> need a reference to the <code>Game</code> model, but the <code>BoardViewModel</code> that was created somehow (see question 1) needs a reference to the <code>Board</code> model that belongs to the <code>Game</code> model. The same goes for all the <code>Tiles</code>. How is all this information passed down the chain? Can I do this much heavy lifting entirely within XAML, or am I going to have to do some sort of binding or other initialization in code?</p>
  
  <p><em>Phew!</em></p>
  
  <p>I appreciate any advice you can give, even if it's not a full answer. I'm also keen to find any examples of MVVM projects that share similar challenges to my own. Thanks a ton!</p>