【问题标题】:Binding errors coming from under hood来自引擎盖下的绑定错误
【发布时间】:2014-12-26 12:00:13
【问题描述】:

尝试在DataGrid 中使用分组并且无缘无故地得到那些绑定错误(它们不属于我的代码,我也看不到处理它们的方法):

System.Windows.Data 错误:4:找不到与引用'RelativeSource FindAncestor,AncestorType='System.Windows.Controls.DataGrid',AncestorLevel='1''的绑定源。绑定表达式:路径=AreRowDetailsFrozen;数据项=空;目标元素是'DataGridDetailsPresenter'(名称='');目标属性是“SelectiveScrollingOrientation”(输入“SelectiveScrollingOrientation”)

System.Windows.Data 错误:4:找不到与引用'RelativeSource FindAncestor,AncestorType='System.Windows.Controls.DataGrid',AncestorLevel='1''的绑定源。 BindingExpression:Path=HeadersVisibility;数据项=空;目标元素是'DataGridRowHeader'(名称='');目标属性是“可见性”(类型“可见性”)

它们出现在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 没有错误;
  • 不绑定(直接设置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 错误:4:找不到与引用'RelativeSource FindAncestor,AncestorType='System.Windows.Controls.DataGrid',AncestorLevel='1''的绑定源。 BindingExpression:Path=HeadersVisibility;数据项=空;目标元素是'DataGridRowHeader'(名称='');目标属性是“可见性”(类型“可见性”)

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

    正在玩控制模板,在为 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>
    

    我从默认模板中删除了DataGridDetailsPresenterDataGridRowHeader,因为我不会使用它们。


    我又犯了一个错误

    System.Windows.Data 错误:4:找不到与引用'RelativeSource FindAncestor,AncestorType='System.Windows.Controls.DataGrid',AncestorLevel='1''的绑定源。 BindingExpression:Path=NewItemMargin;数据项=空;目标元素是'DataGridRow'(名称='');目标属性是“边距”(类型“厚度”)

    我通过将Margin setter 添加到DataGrid.RowStyle 来解决此问题

    <Setter Property="Margin" Value="0"/>
    

    似乎所有这些错误都可以通过重构默认模板来修复。

    【讨论】:

    • 谢谢,伙计!通过谷歌搜索大量“解决方法”后,只有你的方法有效!
    • 也为我工作。谢谢!
    【解决方案2】:

    首先感谢 Sinatr 的问题分析和解决方案,这几乎对我有用。 我确实对 DataGrid 控件有同样的问题。除了输出中的绑定错误之外,新项目行有轻微的位移(在我的例子中,DataRowMargin 偏离了两个 DIP)。

    DataGridRow invalid margin

    整个问题是由 IsNewItemProperty 等于 true 时激活的样式触发器引起的,DataGridRow 没有获得正确的边距值。 Sinatr 的回答确实解决了绑定问题,但仍然使用了错误的边距。这是消除装订错误并设置正确边距的样式。

    <Style TargetType="DataGridRow">
        <Setter Property="Margin" Value="0,0,0,0"/>
        <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.Triggers>
            <Trigger Property="IsNewItem" Value="True">
                <Setter Property="Margin" Value="1,0,0,0"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      相关资源
      最近更新 更多