title: Hooking up the ViewModel to the View in Silverlight
body: <p>I can see two ways to hook up the ViewModel to the View. One is in XAML and the other thru dependancy injection in the code behind.</p>
  
  <p>Which method is more preferable? I prefer the xaml method because I don't want any code in the code behind at all, but is there any issues with one over the other?</p>
  
  <pre><code>&lt;navigation:Page x:Class="MyNamespace.MyViewModel" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:ViewModel="clr-namespace:MyNameSpace.MyViewModel"
     xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
     Title="ViewModel Page" &gt;
  
      &lt;navigation:Page.Resources&gt;
          &lt;ViewModel:MyViewModel x:Key="ViewModel"&gt;&lt;/ViewModel:MyViewModel&gt;
      &lt;/navigation:Page.Resources&gt;
  
      &lt;Grid x:Name="LayoutRoot" Background="White" 
            DataContext="{StaticResource ViewModel}"&gt;
  
      &lt;/Grid&gt;
  &lt;/navigation:Page&gt;
  </code></pre>
  
  <p><strong>OR</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Animation;
  using System.Windows.Shapes;
  using System.Windows.Navigation;
  
  namespace MyNamespace
  {
      public partial class MyView : Page
      {
          public MyView()
          {
              InitializeComponent(MyViewModel viewModel);
  
              this.DataContext = viewModel;
          }
      }
  }
  </code></pre>