1
votes

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')

These errors appear in the immediate window in WPF. Why does this happen, and how can I stop it?

2

2 Answers

2
votes

This post here has details of workarounds for this issue. This one worked for me:

Add the following code to global styles:

<Style.Triggers>
     <Trigger Property="HeadersVisibility" Value="Column">         
          <Setter Property="RowHeaderWidth" Value="0" />
     </Trigger>
</Style.Triggers>
1
votes

Was having the same issue. I made a barebones WPF 4.7 app and in my case the SizeToContent attribute was causing it.

I had a StatusBar with its DataContext set to RelativeSource AncestorType=Window and a DataGrid was a sibling to it both within a DockPanel. The root Window had its SizeToContent set to Width.

When items were added to the DataGrid the binding errors you describe appeared. Without SizeToContent they don't. Using ElementName=rootWindow in the StatusBar instead of the Ancestor shenanigans also fixed it.