title: WPF Datagrid [System.Windows.Data Error: 4]
body: <p>I have a WPF application with a <code>DataGrid</code> as shown below:</p>
  
  <p>Datagrid (<em>simplified</em>):</p>
  
  <pre><code>&lt;DataGrid x:Name="CoreServiceLogDataGrid"
  Grid.Row="0"
  Height="auto"
  ItemsSource="{Binding Source={StaticResource CoreServiceCollection}}"
  AutoGenerateColumns="False"
  CanUserReorderColumns="True"
  CanUserSortColumns="True"
  IsReadOnly="True"&gt;
  
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn x:Name="ID"
              Header="ID"
              Binding="{Binding ID}" /&gt;
  
          &lt;DataGridTextColumn Binding="{Binding Timestamp}"
              Header="Timestamp" /&gt;
  
      &lt;/DataGrid.Columns&gt;
  
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>when the Data is loaded; I get the following Error (numerous times):</p>
  
  <pre><code>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')
  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')
  </code></pre>
  
  <p>I have No Idea why this occurs and how to FIX this.</p>
  
  <p>EDIT: (Info about CoreServiceLogViewCollection)</p>
  
  <p>CoreServiceCollection is just a ListCollectionView. </p>
  
  <pre><code>  public static ListCollectionView CoreServiceLogViewCollection {
          get {
              if (_coreServiceCollection == null) {
                  _coreServiceCollection =
                      new ListCollectionView(LogSession.CoreServiceLogCollection);
              }
  
              return _coreServiceCollection;
          }
      }
  </code></pre>
  
  <p>the parameter is just an <code>ObservableCollection</code> containing <code>ID</code>,<code>Timestamp</code> and other properties</p>
  
  <p>EDIt2: 
  The instantiation is done in App.xaml:</p>
  
  <pre><code>   &lt;ResourceDictionary&gt;
         &lt;x:Static Member="vm2:CoreServiceLogView.CoreServiceLogViewCollection"
                            x:Key="CoreServiceCollection" /&gt;
     &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>EDIT 3 (The Style... )</p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                  mc:Ignorable="d"&gt;
  
  
  &lt;!-- #columnHeaderDGStyle --&gt;
  
  
  &lt;!-- Datagrid --&gt;
  &lt;Style x:Key="Log4NetDataGridStyle"
         TargetType="DataGrid"&gt;
  
      &lt;Setter Property="ColumnHeaderStyle"
              Value="{DynamicResource DatagridColumnHeaderCustomTemplateStyle}" /&gt;
  
  
      &lt;Setter Property="RowStyle"
              Value="{DynamicResource Log4NetRowStyle}" /&gt;
  
      &lt;Setter Property="RowDetailsTemplate"
              Value="{DynamicResource RowDetailsTemplate}" /&gt;
  
      &lt;Setter Property="MaxHeight"
              Value="1600"&gt;
      &lt;/Setter&gt;
      &lt;Setter Property="MaxWidth"
              Value="2560"&gt;
      &lt;/Setter&gt;
  
  
  &lt;/Style&gt;
  
  
  &lt;Style x:Key="DataCommuGridStyle"
         TargetType="DataGrid"&gt;
  
      &lt;Setter Property="ColumnHeaderStyle"
              Value="{DynamicResource DatagridColumnHeaderCustomTemplateStyle}" /&gt;
  
  
      &lt;Setter Property="RowStyle"
              Value="{DynamicResource CommuRowStyle}" /&gt;
  
      &lt;Setter Property="RowDetailsTemplate"
              Value="{DynamicResource RowDetailsTemplate}" /&gt;
  
      &lt;Setter Property="MaxHeight"
              Value="1600"&gt;
      &lt;/Setter&gt;
      &lt;Setter Property="MaxWidth"
              Value="2560"&gt;
      &lt;/Setter&gt;
  
  
  &lt;/Style&gt;
  
  
  &lt;!-- ************************* Row Style ************************* --&gt;
  &lt;Style x:Key="Log4NetRowStyle"
         TargetType="DataGridRow"&gt;
  
      &lt;Setter Property="FontSize"
              Value="14" /&gt;
  
      &lt;Setter Property="Background"
              Value="{Binding Path=LogColour.ColorName}" /&gt;
  
      &lt;Setter Property="Height"
              Value="Auto"&gt;
      &lt;/Setter&gt;
  
      &lt;Style.Triggers&gt;
          &lt;DataTrigger&gt;&lt;/DataTrigger&gt;
      &lt;/Style.Triggers&gt;
  
  &lt;/Style&gt;
  
  &lt;Style x:Key="CommuRowStyle"
         TargetType="DataGridRow"&gt;
  
      &lt;Setter Property="FontSize"
              Value="14" /&gt;
  
      &lt;Setter Property="Background"
              Value="Azure" /&gt;
  
      &lt;Setter Property="Height"
              Value="Auto"&gt;
      &lt;/Setter&gt;
  
      &lt;Style.Triggers&gt;
          &lt;DataTrigger&gt;&lt;/DataTrigger&gt;
      &lt;/Style.Triggers&gt;
  
  &lt;/Style&gt;
  
  
  &lt;DataTemplate x:Key="RowDetailsTemplate"&gt;
      &lt;Border BorderThickness="0"
              Padding="5" &gt;
  
  
          &lt;Border.Background&gt;
              &lt;LinearGradientBrush StartPoint="0,0"
                                   EndPoint="0,1" Opacity="0.2"&gt;
                  &lt;GradientStop Color="White"
                                Offset="0" /&gt;
                  &lt;GradientStop Color="Black"
                                Offset="1" /&gt;
              &lt;/LinearGradientBrush&gt;
  
          &lt;/Border.Background&gt;
  
  
          &lt;!-- alternative with Expancer --&gt;
          &lt;Expander IsExpanded="True"
                    HorizontalAlignment="Left"
                    BorderThickness="1,1,1,5"
                    MaxHeight="300"
                    MaxWidth="900"&gt;
  
              &lt;Expander.Header&gt;
                  &lt;DockPanel&gt;
                      &lt;TextBlock FontSize="12"
                                 Text="LoggingMessage: "
                                 VerticalAlignment="Center" /&gt;
                  &lt;/DockPanel&gt;
              &lt;/Expander.Header&gt;
  
              &lt;Expander.Content&gt;
                  &lt;ScrollViewer VerticalScrollBarVisibility="Auto"
                                HorizontalScrollBarVisibility="Auto"
                                CanContentScroll="True"
                                Style="{StaticResource LeftScrollViewer}"&gt;
                      &lt;StackPanel Orientation="Vertical"&gt;
  
                          &lt;TextBox FontSize="16"
                                   BorderThickness="0"
                                   IsReadOnly="True"
                                   Background="Transparent"
                                   Foreground="Black"
                                   TextWrapping="Wrap"
                                   Text="{Binding LoggingMessage, Mode=OneWay}" /&gt;
                      &lt;/StackPanel&gt;
                  &lt;/ScrollViewer&gt;
              &lt;/Expander.Content&gt;
          &lt;/Expander&gt;
  
  
      &lt;/Border&gt;
  
  
  &lt;/DataTemplate&gt;
  
  
  &lt;Style x:Key="GroupHeaderStyle"
         TargetType="{x:Type GroupItem}"&gt;
      &lt;Setter Property="Margin"
              Value="0,0,0,5" /&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type GroupItem}"&gt;
                  &lt;Expander IsExpanded="False"
                            Background="#FF112255"
                            BorderBrush="#FF002255"
                            Foreground="Black"
                            BorderThickness="1,1,1,5"&gt;
                      &lt;Expander.Header&gt;
                          &lt;DockPanel&gt;
                              &lt;TextBlock FontWeight="Bold"
                                         Foreground="White"
                                         Text="{Binding Path=Name}"
                                         Margin="5,0,0,0"
                                         Width="100" /&gt;
                              &lt;TextBlock FontWeight="Bold"
                                         Foreground="White"
                                         Text="{Binding Path=ItemCount}" /&gt;
                          &lt;/DockPanel&gt;
                      &lt;/Expander.Header&gt;
  
                      &lt;Expander.Content&gt;
                          &lt;ItemsPresenter /&gt;
                      &lt;/Expander.Content&gt;
                  &lt;/Expander&gt;
  
  
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  
  
  &lt;!-- ******************** DataTemplate ******************** --&gt;
  
  
  &lt;/ResourceDictionary&gt;
  </code></pre>