首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WPF DataBinding错误:找不到使用引用“”RelativeSource FindAncestor“”绑定的源

WPF DataBinding错误:找不到使用引用“”RelativeSource FindAncestor“”绑定的源
EN

Stack Overflow用户
提问于 2010-10-09 04:14:51
回答 2查看 8.1K关注 0票数 10

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

代码语言:javascript
运行
复制
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')
代码语言:javascript
运行
复制
<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>
代码语言:javascript
运行
复制
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;
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-11 22:54:21

这是一个已知的错误;有关更多详细信息,请查看http://wpf.codeplex.com/discussions/47047http://social.msdn.microsoft.com/Forums/en-GB/wpf/thread/af7cd462-febe-482b-9a04-61b076933c7b

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

票数 7
EN

Stack Overflow用户

发布于 2010-10-10 04:19:43

首先,WPF数据网格的行默认情况下是白色的,那么为什么要在您的样式中将它们设置为白色?您可以完全删除DataGrid.Resources位,并将AlternationCount=2替换为AlternatingRowBackground="FFCAC6C6“(尽管这会导致第一行为白色,第二行为彩色,等等。如果这不能接受,您仍然可以删除将背景设置为白色的触发器)。

关于错误-由于您提供的代码不包含任何设置了RelativeSource的绑定,因此我只能得出两点结论:

1)要么你没有提供完整的代码,要么你需要重新检查你的绑定,其中有RelativeSource,因为很明显有一个错误。

2)你没有使用WPF的内置DataGrid。也许是codeplex的WPF toolkit DataGrid?虽然我认为它也不应该有这些错误,但它更有可能再次出现结论1。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3893990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档