本文介绍了WPF 数据绑定错误:无法找到引用“RelativeSource FindAncestor"的绑定源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I get the following errors from the code below... not sure why (and yes, it produces all 4 even though it's the same 2 repeated). Oh, and it doesn't produce the alternating rows effect, even though prior to these errors popping up the same code was working.

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

推荐答案

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

This is a known bug; check http://wpf.codeplex.com/discussions/47047 and http://social.msdn.microsoft.com/Forums/en-GB/wpf/thread/af7cd462-febe-482b-9a04-61b076933c7b for more details.

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

In the first URL (Codeplex) I am going to post a workaround; however, it involves modification of the WPF Toolkit source code.

这篇关于WPF 数据绑定错误:无法找到引用“RelativeSource FindAncestor"的绑定源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:42