title: Silverlight - Setting DataContext in XAML rather than in constructor?
body: <p>How can I set the DataContext on my Grid in XAML, instead of in the constructor?</p>
  
  <p>Here is how I do it in the constructor (LayoutRoot is the XAML Grid defined in the XAML):</p>
  
  <pre><code>this.LayoutRoot.DataContext = this.HPVM;
  </code></pre>
  
  <p>I would prefer to do it right in the XAML, but I do not know how to reference the HPVM object in XAML.  HPVM is a public property on the USerControl class.</p>
  
  <p>It works fine as listed above, but again, I just want to know how to properties of the UserControl class in XAML, rather than always having to do it in code.</p>
  
  <p>Here is all the relevant code:</p>
  
  <pre><code>  &lt;UserControl x:Class="SilverlightApplication1.SLHolePattern" x:Name="HolePatternsControl"
      xmlns="http://schemas.microsoft.com/client/2007"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:sys="clr-namespace:System;assembly=mscorlib"    
      xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"
      xmlns:local="clr-namespace:SilverlightApplication1"    
      xmlns:GeoPatterns="clr-namespace:GeoPatterns"
      Height="700"&gt;
  
  
      &lt;UserControl.Resources&gt;
      ...
  </code></pre>
  
  <p>And here is my constructor where the DataContext is currently set:</p>
  
  <pre><code>namespace SilverlightApplication1
  {
      public partial class SLHolePattern : UserControl, INotifyPropertyChanged
      {
          public HolePatternsViewModel HPVM;
  
          public SLHolePattern()
          {
              InitializeComponent();
  
              this.HPVM=new HolePatternsViewModel();
              this.LayoutRoot.DataContext = this.HPVM;
              ...more code here
          }
  </code></pre>
  
  <p>It all works fine, but I just want to learn how to set the DataContext in XAML, not in code.</p>