WPF数据绑定错误:无法找到相对源为“RelativeSource FindAncestor”的绑定引用。

10
我从下面的代码中获取以下错误... 不确定原因(尽管它是相同的 2 个重复,但会生成所有 4 个)。哦,而且它不会产生交替行效果,尽管在这些错误出现之前相同的代码是有效的。
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')
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')
<UserControl x:Class="MyProject.Views.RegistrationAllView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyProject.Views"
             >
    <Grid>
        <DataGrid Name="TestGrid" Grid.Row="2" Grid.ColumnSpan="2" AutoGenerateColumns="True"
                  ItemsSource="{Binding Registrations}" SelectedValue="{Binding CurrentRegistration}" IsReadOnly="True" GridLinesVisibility="None"
                  AlternatingRowBackground="#FFCAC6C6"
                  >
            <DataGrid.RowStyle>
                <Style>
                    <EventSetter Event="DataGridRow.MouseDoubleClick" Handler="TestGrid_MouseDoubleClick" />
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
    </Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using MyProject.ViewModels;

using WPFBase;
using WPFBase.ViewModels;

namespace MyProject.Views
{
    public partial class RegistrationAllView : UserControl
    {
        public RegistrationAllView()
        {
            InitializeComponent();
        }

        private void TestGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DependencyObject source = e.OriginalSource as DependencyObject;

            RegistrationEntity entity = (RegistrationEntity)TestGrid.CurrentItem;

            TabControl TabCollection = (TabControl)UIHelper.TryFindParentControl<TabControl>(this);

            RegistrationForm view = new RegistrationForm();

            XTabItem tabItem = new XTabItem();
            tabItem.Header = String.Format("Registration (#{0})", entity.ID);
            tabItem.Content = view;

            TabCollection.Items.Add(tabItem);

            tabItem.Focus();

            AbstractViewModel vm = new RegistrationViewModel(entity);

            view.DataContext = vm;
        }
    }
}

你的ItemsSource中对象的DataTemplate在哪里? - Robert Rossney
@Robert Rossney:我将其设置为AutoGenerateColumns,因此我实际上不需要DataTemplate。 - myermian
2个回答

7

非常感谢您提供的链接。在我的情况下,由于无法破解WPF,因此我选择了顶部第9篇帖子中的解决方案:“我发现唯一的解决方法是将HeadersVisibility设置为“All”,并将RowHeaderWidth设置为“0”。”。这是我首选的解决方案。 - Barton

0
首先,WPF数据网格的行默认是白色的,那么为什么你要在样式中将它们设置为白色呢?你可以完全摆脱DataGrid.Resources部分,并用AlternatingRowBackground="FFCAC6C6"替换AlternationCount=2(尽管这会导致第一行是白色的,第二行是彩色的等等。如果这不可接受,您仍然可以删除将背景设置为白色的触发器)。
关于错误 - 由于您提供的代码没有设置RelativeSource的绑定,我只能得出两个结论:
1)要么您没有提供完整的代码,需要重新查看其中包含RelativeSource的绑定,因为显然某处存在错误。
2)您没有使用WPF内置的DataGrid。也许是来自codeplex的WPF工具包DataGrid?虽然我认为它也不应该有这些错误,但更可能是第一种情况。

1
我已经发布了整个代码,尽管实际上没有太多需要发布的。我按照建议对AlternatingRowBackground进行了更改,它可以正常工作并且更加紧凑。虽然我仍然会收到错误信息,但它们似乎并不影响功能或显示。哦,我正在使用内置的Datagrid(而不是来自WPF Toolkit的Datagrid)。 - myermian
必须有一些代码片段与祖先查找模式绑定。1)可能是在xmlns:local =“clr-namespace:MyProject.Views”中的某个类?2)或者某个模板被覆盖了... 3)如果从datagrid中删除ItemsSource和SelectedValue绑定,错误是否消失?4)我认为应该有一种方法来增加数据绑定错误的详细程度,请搜索一下,也许您会得到更多关于这些错误抛出位置的线索。5)由于这是一个无法单独调试的用户控件,也许错误来自于托管窗口? - Marko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接