title: WPF DataGrid and DataGridTemplateColumn Header Problems
body: <p>I have an issue with a piece of code...</p>
  
  <p>I have something like as follows:</p>
  
  <pre><code>&lt;DataGrid AutoGenerateColumns="False"
            CanUserAddRows="False"
            CanUserDeleteRows="False"
            ItemsSource="{Binding Path=RowData,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            RowHeaderWidth="0"&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTemplateColumn&gt;
              &lt;DataGridTemplateColumn.Header&gt;
                  &lt;CheckBox IsChecked="{Binding RelativeSource={RelativeSource Findancestor, AncestorType={x:Type UserControl}},Path=DataContext.AreAllSelected}"&gt;
                      Select All
                  &lt;/CheckBox&gt;
              &lt;/DataGridTemplateColumn.Header&gt;
              &lt;DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;CheckBox IsChecked="{Binding Path=IsSelected,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGridTemplateColumn.CellTemplate&gt;
          &lt;/DataGridTemplateColumn&gt;  
      &lt;DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>It is in a View that is derived from the type UserControl -- for absolute sure.  For some reason, the binding fails on the CheckBox in the TemplateColumn Header.  What's unusual is that I have this code in other views -- and it works absolutely fine and without any issues.  I always create the ViewModel first, and pass it in as a parameter to the View, wherein the DataContext is set to the ViewModel upon construction, i.e.</p>
  
  <pre><code>View(ViewModel myViewModel)
  {
     InitializeComponent();
     DataContext = myViewModel;
  }
  </code></pre>
  
  <p>The ViewModel has the AreAllSelected property on it, but I get the following error -- so I know it isn't finding the ancestor for some reason.</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 4 : Cannot find source for binding with
    reference 'RelativeSource FindAncestor,
    AncestorType='System.Windows.Controls.UserControl',
    AncestorLevel='1''. BindingExpression:Path=DataContext.AreAllSelected;
    DataItem=null; target element is 'CheckBox' (Name=''); target property
    is 'IsChecked' (type 'Nullable`1')</p>
  </blockquote>
  
  <p>The weird thing is that, even though it doesn't work, when I use Snoop to view it, it will initially not work.  When I look into the Binding and delve into it, the Binding now works.</p>
  
  <p>I'm wondering if it's some weird issue with how it's constructed at a Visual Tree level or what?  As I said prior -- I've done this several other times in other Views and they work fine.  There must be some weird thing I'm doing incorrectly...</p>