wpf 无法找到引用“RelativeSource FindAncestor, AncestorType='MahApps.Metro.Controls.Glow', AncestorLevel='1'”的绑定源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17366887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='MahApps.Metro.Controls.Glow', AncestorLevel='1''
提问by Dinesh
I've created one wpf application in which I've used MahApps Metro tools for my view window. My application is working perfectly, but binding error is shown in output window. I've not used any code which is mentioned in that error.
我已经创建了一个 wpf 应用程序,其中我在视图窗口中使用了 MahApps Metro 工具。我的应用程序运行良好,但输出窗口中显示绑定错误。我没有使用该错误中提到的任何代码。
Error is:
错误是:
Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='MahApps.Metro.Controls.Glow', AncestorLevel='1''. BindingExpression:Path=GlowColor; DataItem=null; target element is 'SolidColorBrush' (HashCode=9047482); target property is 'Color' (type 'Color')
无法找到引用“RelativeSource FindAncestor, AncestorType='MahApps.Metro.Controls.Glow', AncestorLevel='1'”的绑定源。BindingExpression:Path=GlowColor; 数据项=空;目标元素是“SolidColorBrush”(HashCode=9047482);目标属性是“颜色”(类型“颜色”)
xaml code:
xml代码:
<Controls:MetroWindow
x:Name="MainWin"
x:Class="TimeSheet.DayView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:TimeSheet.Views.DataTemplateSpace"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
Title="DayView" Width="596" Height="596">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="DefaultDataTemplate">
<StackPanel Orientation="Horizontal" Width="596">
<TextBox Text="{Binding ClientNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
<TextBox Text="{Binding ApplicationNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
<TextBox Text="{Binding StartTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
<TextBox Text="{Binding StopTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="60"/>
<TextBox Text="{Binding ProjectNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="130"/>
<TextBox Text="{Binding TaskNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="71"/>
</StackPanel>
</DataTemplate>
<StackPanel Orientation="Horizontal" Margin="-1,93,1,434" RenderTransformOrigin="1.155,0.47" Height="25">
<TextBox Text="Client" HorizontalContentAlignment="Center" Width="145" Foreground="White" Background="Blue" Padding="0" BorderThickness="0" VerticalContentAlignment="Center"/>
<TextBox Text="Application" HorizontalContentAlignment="Center" Width="90" Foreground="White" Background="Blue" Padding="0" BorderThickness="0" VerticalContentAlignment="Center"/>
<TextBox Text="StartTime" HorizontalContentAlignment="Center" Canvas.Left="148" Canvas.Top="86" Width="100" Foreground="White" Background="Blue" Padding="0" BorderThickness="0" RenderTransformOrigin="0.5,0.5" VerticalContentAlignment="Center"/>
<TextBox Text="StopTime" HorizontalContentAlignment="Center" Width="60" RenderTransformOrigin="0.471,0.692" Foreground="White" Background="Blue" Padding="0" BorderThickness="0" VerticalContentAlignment="Center"/>
<TextBox Text="Task" HorizontalContentAlignment="Center" Canvas.Left="378" Canvas.Top="86" Width="130" Foreground="White" Background="Blue" Padding="0" BorderThickness="0" VerticalContentAlignment="Center"/>
<TextBox Text="Project" HorizontalContentAlignment="Center" Width="71" Foreground="White" Background="Blue" Padding="0" BorderThickness="0" VerticalContentAlignment="Center"/>
</StackPanel>
<ListBox x:Name="listBox1" ItemsSource="{Binding}" Margin="0,131,0,59" ItemTemplateSelector="{StaticResource templateSelector}" SelectionMode="Single">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="MouseDoubleClick" Handler="listBox1_MouseDoubleClick">
</EventSetter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Controls:MetroWindow>
回答by Rachel
It sounds like a binding error related to your global styles, templates, or user-controls.
这听起来像是与您的全局样式、模板或用户控件相关的绑定错误。
I wrote up something about debugging binding errors here.
我在这里写了一些关于调试绑定错误的内容。
To summarize, it's easiest if you add line-breaks at the semi-colons and commas, and read the error backwards
总而言之,最简单的方法是在分号和逗号处添加换行符,然后向后阅读错误
Do that with your error, and you get this:
用你的错误来做,你会得到这个:
target property is 'Color' (type 'Color')
target element is 'SolidColorBrush' (HashCode=9047482);
DataItem=null;
Cannot find source for binding with reference 'RelativeSource FindAncestor,
AncestorType='MahApps.Metro.Controls.Glow',
AncestorLevel='1''.
BindingExpression:Path=GlowColor;
It tells you that:
它告诉你:
Somewhere you have a
Colorproperty causing a binding error.That property is on a
SolidColorBrushobjectThe
DataContextof that item isnull.And the binding it's having problems evaluating is a
RelativeSourcebinding that is looking for aMahApps.Metro.Controls.Glowobject further up the visual tree so it can find theGlowColorproperty of that object and use it.
某处你有一个
Color属性导致绑定错误。该属性在一个
SolidColorBrush对象上该
DataContext项目的 是null。并且它在评估时遇到问题的
RelativeSource绑定是一个绑定,MahApps.Metro.Controls.Glow它在可视化树的更上方寻找一个对象,以便它可以找到该GlowColor对象的属性并使用它。
Try doing a search on your application for GlowColorand see if you can find it. It's most likely in your Colours.xamlfile, since that probably contains your SolidColorBrushobjects.
尝试搜索您的应用程序GlowColor,看看是否可以找到它。它很可能在您的Colours.xaml文件中,因为它可能包含您的SolidColorBrush对象。

