Trying to use grouping in DataGrid
and for no reasons getting those binding errors (they are not belong to my code, nor I see a way to deal with them):
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')
and
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')
They appears for each row in DataGrid
. This bugs me alot!
To reproduce the problem I made a small project
public class MyItem
{
public string A { get; set; }
}
public class ViewModel
{
public List<MyItem> List { get; set; }
public ViewModel()
{
List = new List<MyItem>(new[] { new MyItem() });
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
xaml
<DataGrid ItemsSource="{Binding List}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding A}" Header="A"/>
</DataGrid.Columns>
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="GroupItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GroupItem">
<!-- anything or nothing here -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
Some observations:
- without
DataGrid.GroupStyle
there are no errors; - with
AutoGenerateColumns = true
there are no errors; - without binding (setting
DataGrid.ItemsSource
directly) there are no errors.
Only combination of opposite to those 3 conditions will start spamming Output
window with above messages.
What should I do? I can't ignore errors, nor I see a way to fix them.
Googling around wasn't really helpful, to example, this case was called a bug, I tried to apply its workarounds, but none works for me.
P.S.: discovering such bugs at first attempt to use DataGrid
is very demotivating.
Trying to deal with second error.
<DataGrid.RowHeaderStyle>
<Style TargetType="DataGridRowHeader">
<Setter Property="Visibility" Value="Collapsed"/>
<Setter Property="Template" Value="{x:Null}"/>
</Style>
</DataGrid.RowHeaderStyle>
But error still
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')