WPF DataBinding错误:找不到引用'RelativeSource FindAncestor'的绑定源

时间:2010-10-08 20:14:51

标签: wpf data-binding wpfdatagrid

我从下面的代码中得到以下错误...不确定为什么(是的,它产生所有4,即使它是相同的2重复)。哦,并且它不会产生交替的行效果,即使在这些错误出现之前弹出相同的代码也是有效的。

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;
        }
    }
}

2 个答案:

答案 0 :(得分:7)

这是一个已知的错误;查看http://wpf.codeplex.com/discussions/47047http://social.msdn.microsoft.com/Forums/en-GB/wpf/thread/af7cd462-febe-482b-9a04-61b076933c7b了解详情。

在第一个URL(Codeplex)中,我将发布一个解决方法;但是,它涉及修改WPF Toolkit源代码。

答案 1 :(得分:0)

首先,WPF数据网格的行默认为白色,那你为什么要在你的风格中将它们设置为白色?您可以完全摆脱DataGrid.Resources位并用AlternatingRowBackground =“FFCAC6C6”替换AlternationCount = 2(虽然这会导致第一行为白色而第二行为彩色等。如果这是不可接受的,您仍然可以删除将背景设置为白色的触发器。

关于错误 - 由于您提供的代码不包含与RelativeSource集的任何绑定,我只能得出两件事:

1)你没有提供完整的代码,你需要重新查看其中包含RelativeSource的绑定,因为显然在某处有错误。

2)您没有使用WPF的内置DataGrid。也许来自codeplex的WPF工具包DataGrid?虽然我相信它也不应该有这些错误,但它更有可能再次得出结论。