title: Silverlight: Binding to a LayoutRoot value from within a DataTemplate
body: <p>I have a <code>DataTemplate</code> for a <code>ListBox</code>, where I have several controls that bind to an item. I would also like to bind to a value on <code>LayoutRoot.DataContext</code>. I'm unsure of how to do this.</p>
  
  <pre><code>&lt;!--LayoutRoot is the root grid where all page content is placed--&gt;
  &lt;StackPanel x:Name="LayoutRoot" Background="Transparent"&gt;
  
      &lt;ListBox ItemsSource="{Binding Items}"&gt;
          &lt;ListBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;StackPanel&gt;
                      &lt;TextBlock Text="{Binding}" /&gt;
                      &lt;TextBlock Text="{Binding ElementName=LayoutRoot, Path=DataContext.Foo}" /&gt;
                  &lt;/StackPanel&gt;
              &lt;/DataTemplate&gt;
          &lt;/ListBox.ItemTemplate&gt;
      &lt;/ListBox&gt;
  
  &lt;/StackPanel&gt;
  
  public partial class MainPage : PhoneApplicationPage
  {
      public string Foo
      {
          get
          {
              return "the moon";
          }
      }
  
      private int startIndex = 1;
  
      private IList&lt;string&gt; _data = new List&lt;string&gt;() { "foo", "bar", "baz" };
      public IList&lt;string&gt; Items
      {
          get
          {
              return _data;
          }
      }
  
      // Constructor
      public MainPage()
      {
          InitializeComponent();
          LayoutRoot.DataContext = this;
      }
  }
  </code></pre>
  
  <p>This doesn't work; only the <code>_data</code> items are displayed. The following binding errors appear in the Debug output:</p>
  
  <pre><code>System.Windows.Data Error: BindingExpression path error: 'Foo' property not found on 'foo' 'System.String' (HashCode=1502598398). BindingExpression: Path='DataContext.Foo' DataItem='System.Windows.Controls.Border' (HashCode=78299055); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..
  System.Windows.Data Error: BindingExpression path error: 'Foo' property not found on 'bar' 'System.String' (HashCode=696029481). BindingExpression: Path='DataContext.Foo' DataItem='System.Windows.Controls.Border' (HashCode=78298703); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..
  System.Windows.Data Error: BindingExpression path error: 'Foo' property not found on 'baz' 'System.String' (HashCode=696029489). BindingExpression: Path='DataContext.Foo' DataItem='System.Windows.Controls.Border' (HashCode=78298694); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..
  </code></pre>
  
  <p>Do I have a syntax error somewhere?</p>
  
  <p><strong>Update</strong> I'm aiming for something that looks like this:</p>
  
  <ul>
  <li>foo</li>
  <li>the moon</li>
  <li>bar</li>
  <li>the moon</li>
  <li>baz</li>
  <li>the moon</li>
  </ul>
  
  <p>Instead, all I'm getting is:</p>
  
  <ul>
  <li>foo</li>
  <li>bar</li>
  <li>baz</li>
  </ul>
  
  <p>(I'm using Silverlight for Windows Phone 7.)</p>