C# 找不到引用为的绑定源';RelativeSource FindAncestor,AncestorType=&x27;MahApps.Metro.Controls.Glow';,AncestorLevel=&x27;1'';
我已经创建了一个wpf应用程序,其中我使用了MahApps Metro工具作为我的视图窗口。我的应用程序工作正常,但绑定错误显示在输出窗口中。我没有使用那个错误中提到的任何代码 错误是: 找不到引用为“RelativeSource”的绑定源 FindAncestor,AncestorType='MahApps.Metro.Controls.Glow', AncestorLevel='1'。BindingExpression:Path=GlowColor;DataItem=null; 目标元素是“SolidColorBrush”(HashCode=9047482);目标 属性为“颜色”(类型为“颜色”) xaml代码:C# 找不到引用为的绑定源';RelativeSource FindAncestor,AncestorType=&x27;MahApps.Metro.Controls.Glow';,AncestorLevel=&x27;1'';,c#,wpf,xaml,relativesource,findancestor,C#,Wpf,Xaml,Relativesource,Findancestor,我已经创建了一个wpf应用程序,其中我使用了MahApps Metro工具作为我的视图窗口。我的应用程序工作正常,但绑定错误显示在输出窗口中。我没有使用那个错误中提到的任何代码 错误是: 找不到引用为“RelativeSource”的绑定源 FindAncestor,AncestorType='MahApps.Metro.Controls.Glow', AncestorLevel='1'。BindingExpression:Path=GlowColor;DataItem=null; 目标元素是“
<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>
听起来像是与全局样式、模板或用户控件相关的绑定错误 我写了一些关于调试绑定错误的东西 总而言之,如果在分号和逗号处添加换行符,并向后读取错误,则最简单 用你的错误去做,你会得到这样的结果: 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; 目标属性为“颜色”(类型为“颜色”) 目标元素是“SolidColorBrush”(HashCode=9047482); DataItem=null; 找不到引用为“RelativeSource FindAncestor”的绑定源, AncestorType='MahApps.Metro.Controls.Glow', AncestorLevel='1'。 BindingExpression:Path=GlowColor; 它告诉你:
- 某个地方有一个
属性导致绑定错误Color
- 该属性位于
对象上SolidColorBrush
- 该项的
为DataContext
null
- 它在评估时遇到问题的绑定是一个
绑定,该绑定正在视觉树的更上层查找RelativeSource
对象,以便它可以找到该对象的MahApps.Metro.Controls.Glow
属性并使用它GlowColor
GlowColor
,看看是否可以找到它。它最有可能出现在colors.xaml
文件中,因为该文件可能包含SolidColorBrush
对象