title: Binding errors coming from under hood
body: <p>Trying to use grouping in <code>DataGrid</code> and for no reasons getting those binding errors (they are not belong to my code, nor I see a way to deal with them):</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=AreRowDetailsFrozen; DataItem=null; target element is 'DataGridDetailsPresenter' (Name=''); target property is 'SelectiveScrollingOrientation' (type 'SelectiveScrollingOrientation')</p>
  </blockquote>
  
  <p>and</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=HeadersVisibility; DataItem=null; target element is 'DataGridRowHeader' (Name=''); target property is 'Visibility' (type 'Visibility')</p>
  </blockquote>
  
  <p>They appears for <strong>each row</strong> in <code>DataGrid</code>. This bugs me alot!</p>
  
  <p>To reproduce the problem I made a small project</p>
  
  <pre><code>public class MyItem
  {
      public string A { get; set; }
  }
  
  public class ViewModel
  {
      public List&lt;MyItem&gt; List { get; set; }
  
      public ViewModel()
      {
          List = new List&lt;MyItem&gt;(new[] { new MyItem() });
      }
  }
  
  public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
          DataContext = new ViewModel();
      }
  }
  </code></pre>
  
  <p>xaml</p>
  
  <pre><code>&lt;DataGrid ItemsSource="{Binding List}" AutoGenerateColumns="False"&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn Binding="{Binding A}" Header="A"/&gt;
      &lt;/DataGrid.Columns&gt;
      &lt;DataGrid.GroupStyle&gt;
          &lt;GroupStyle&gt;
              &lt;GroupStyle.ContainerStyle&gt;
                  &lt;Style TargetType="GroupItem"&gt;
                      &lt;Setter Property="Template"&gt;
                          &lt;Setter.Value&gt;
                              &lt;ControlTemplate TargetType="GroupItem"&gt;
                                  &lt;!-- anything or nothing here --&gt;
                              &lt;/ControlTemplate&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                  &lt;/Style&gt;
              &lt;/GroupStyle.ContainerStyle&gt;
          &lt;/GroupStyle&gt;
      &lt;/DataGrid.GroupStyle&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>Some observations:</p>
  
  <ul>
  <li>without <code>DataGrid.GroupStyle</code> there are no errors;</li>
  <li>with <code>AutoGenerateColumns = true</code> there are no errors;</li>
  <li>without binding (setting <code>DataGrid.ItemsSource</code> directly) there are no errors.</li>
  </ul>
  
  <p>Only combination of opposite to those 3 conditions will start spamming <code>Output</code> window with above messages.</p>
  
  <p>What should I do? I can't ignore errors, nor I see a way to fix them.</p>
  
  <p>Googling around wasn't really helpful, to example, <a href="https://stackoverflow.com/q/3893990/1997232">this case</a> was called a bug, I tried to apply its workarounds, but none works for me.</p>
  
  <p>P.S.: discovering such bugs at first attempt to use <code>DataGrid</code> is very demotivating.</p>
  
  <hr>
  
  <p>Trying to deal with second error.</p>
  
  <pre><code>&lt;DataGrid.RowHeaderStyle&gt;
      &lt;Style TargetType="DataGridRowHeader"&gt;
          &lt;Setter Property="Visibility" Value="Collapsed"/&gt;
          &lt;Setter Property="Template" Value="{x:Null}"/&gt;
      &lt;/Style&gt;
  &lt;/DataGrid.RowHeaderStyle&gt;
  </code></pre>
  
  <p>But error still</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=HeadersVisibility; DataItem=null; target element is 'DataGridRowHeader' (Name=''); target property is 'Visibility' (type 'Visibility')</p>
  </blockquote>