尝试在 DataGrid
中使用分组,但无缘无故地遇到这些绑定(bind)错误(它们不属于我的代码,我也没有找到处理它们的方法):
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')
它们出现在 DataGrid
中的每一行。这让我很烦恼!
为了重现这个问题我做了一个小项目
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>
一些观察:
- 没有
DataGrid.GroupStyle
就没有错误; AutoGenerateColumns = true
没有错误;- 没有绑定(bind)(直接设置
DataGrid.ItemsSource
)没有错误。
只有与这 3 个条件相反的组合才会开始向 Output
窗口发送上述消息。
我该怎么办?我无法忽略错误,也无法找到修复它们的方法。
谷歌搜索并没有多大帮助,例如 this case被称为错误,我尝试应用它的解决方法,但没有一个对我有用。
P.S.:在第一次尝试使用 DataGrid
时发现此类错误非常令人沮丧。
正在尝试处理第二个错误。
<DataGrid.RowHeaderStyle>
<Style TargetType="DataGridRowHeader">
<Setter Property="Visibility" Value="Collapsed"/>
<Setter Property="Template" Value="{x:Null}"/>
</Style>
</DataGrid.RowHeaderStyle>
但还是报错
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')
最佳答案
正在玩控件模板,在为 DataGridRow
更改一个模板后,错误消失了!
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridRow">
<Border BorderThickness="{TemplateBinding Border.BorderThickness}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="DGR_Border" SnapsToDevicePixels="True">
<SelectiveScrollingGrid>
<DataGridCellsPresenter ItemsPanel="{TemplateBinding ItemsControl.ItemsPanel}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/>
</SelectiveScrollingGrid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
我从默认模板中删除了 DataGridDetailsPresenter
和 DataGridRowHeader
,因为我不会使用它们。
还有一个错误
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=NewItemMargin; DataItem=null; target element is 'DataGridRow' (Name=''); target property is 'Margin' (type 'Thickness')
我通过将 Margin
setter 添加到 DataGrid.RowStyle
中来解决这个问题
<Setter Property="Margin" Value="0"/>
似乎所有这些错误都可以通过重构默认模板来修复。
关于c# - 来自幕后的绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26653099/