title: Why does binding the MainWindow datacontext in XAML fail to act the same as binding in the codebehind with this.datacontext=this?
body: <p>I am trying to use Data binding to bind an ObservableCollection to the ItemsSource of a DataGrid, as I learn about WPF and stuff.</p>
  
  <p>In the code-behind I can set the DataContext with <code>this.DataContext = this;</code> or <code>bloopDataGrid.DataContext = this;</code>. That's fine and dandy.</p>
  
  <p>I thought I could try something like</p>
  
  <pre><code>&lt;Window.DataContext&gt;
      &lt;local:MainWindow/&gt;
  &lt;/Window.DataContext&gt;
  </code></pre>
  
  <p>in my main window, but this causes a Stack Overflow Exception as explained in <a href="https://stackoverflow.com/q/14152222/1751715">this question</a>. Fine, that makes some sense.</p>
  
  <p>After reading <a href="https://stackoverflow.com/a/12848253/1751715">this</a> and other questions/answers that say to try <code>DataContext="{Binding RelativeSource={RelativeSource Self}}"</code> in the window's XAML code, I thought I could <em>actually do this</em>. Apparently I <strong>cannot</strong>. Or at least, the IDE lets me and it's syntactically correct, but does not do what I want (ie, exactly what <code>this.DataContext = this;</code> does).</p>
  
  <p>Then I read <a href="https://stackoverflow.com/a/10510858/1751715">this</a> about using <code>"{Binding ElementName=, Path=}"</code> and tried to use it like so:</p>
  
  <pre><code>&lt;DataGrid
      Name="bloopDataGrid"
      Grid.Row="1"
      ItemsSource="{Binding ElementName=testWin, Path=OutputCollection}"&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>Which also doesn't work. Maybe not for the same reason, but I can't figure out the problem with it. </p>
  
  <p>Oddly, I can't replicate the rebinding example shown in <a href="http://rachel53461.wordpress.com/2012/07/14/what-is-this-datacontext-you-speak-of/" rel="nofollow noreferrer">Rachel Lim's blog post</a>.</p>
  
  <p><strong>XAML:</strong></p>
  
  <pre><code>&lt;Window
      x:Class="DataBinding.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow"
      Height="350"
      Width="525"
      x:Name="testWin"&gt;
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition /&gt;
              &lt;RowDefinition /&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;Label Grid.Row="0" Content="{Binding text}"&gt;   
          &lt;/Label&gt;
  
          &lt;DataGrid
              Name="bloopDataGrid"
              Grid.Row="1"
              ItemsSource="{Binding Path=OutputCollection}"&gt;
          &lt;/DataGrid&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>C#:</strong></p>
  
  <pre><code>using System;
  using System.Collections.ObjectModel; //For ObservableCollection&lt;T&gt;
  using System.Windows;
  
  namespace DataBinding
  {
      public partial class MainWindow : Window
      {
          public String text { get; set; }
          public ObservableCollection&lt;testStruct&gt; OutputCollection { get; set; }
  
          public struct testStruct
          {
              public testStruct(String x, String y) : this()
              {
                  Col1 = x;
                  Col2 = y;
              }
              public String Col1 { get; set; }
              public String Col2 { get; set; }
          }
  
          public MainWindow()
          {
              InitializeComponent();
  
              testA t1 = new testA();
              this.DataContext = this;
              //this.DataContext = t1;
              //bloopDataGrid.DataContext = this;
              text = "bound \"this\"";
              t1.text = "bound a class";
  
              OutputCollection = new ObservableCollection&lt;testStruct&gt;();
              OutputCollection.Add(new testStruct("1", "2"));
              OutputCollection.Add(new testStruct("3", "4"));
          }
  
          public class testA
          {
              public String text { get; set; }
          }
  
      }
  }
  </code></pre>
  
  <p>The above code is what I'm using to test this, and is currently using the code-behind version which correctly gives me</p>
  
  <p><img src="https://i.stack.imgur.com/Sb84o.png" alt="In codebehind"></p>
  
  <p>What am I doing wrong, which is preventing me from getting the same results as the above picture but by using XAML for the DataContext handling? Am I not connecting the dots properly? ...am I missing some dots?</p>