我从下面的代码中得到了以下错误...不确定为什么(是的,它会生成所有的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;
}
}
}
发布于 2011-07-11 22:54:21
这是一个已知的错误;有关更多详细信息,请查看http://wpf.codeplex.com/discussions/47047和http://social.msdn.microsoft.com/Forums/en-GB/wpf/thread/af7cd462-febe-482b-9a04-61b076933c7b。
在第一个URL (Codeplex)中,我将发布一个变通方法;但是,它涉及到对WPF Toolkit源代码的修改。
发布于 2010-10-10 04:19:43
首先,WPF数据网格的行默认情况下是白色的,那么为什么要在您的样式中将它们设置为白色?您可以完全删除DataGrid.Resources位,并将AlternationCount=2替换为AlternatingRowBackground="FFCAC6C6“(尽管这会导致第一行为白色,第二行为彩色,等等。如果这不能接受,您仍然可以删除将背景设置为白色的触发器)。
关于错误-由于您提供的代码不包含任何设置了RelativeSource的绑定,因此我只能得出两点结论:
1)要么你没有提供完整的代码,要么你需要重新检查你的绑定,其中有RelativeSource,因为很明显有一个错误。
2)你没有使用WPF的内置DataGrid。也许是codeplex的WPF toolkit DataGrid?虽然我认为它也不应该有这些错误,但它更有可能再次出现结论1。
https://stackoverflow.com/questions/3893990
复制相似问题