2013-06-28 68 views
3

我創建了一個wpf應用程序,其中我使用了MahApps Metro工具查看窗口。我的應用程序運行正常,但輸出窗口中顯示綁定錯誤。我沒有使用任何在該錯誤中提到的代碼。找不到綁定的源代碼'RelativeSource FindAncestor,AncestorType ='MahApps.Metro.Controls.Glow',AncestorLevel ='''

錯誤是:

不能用於與參考1 ' '結合 '的RelativeSource FindAncestor,AncestorType =' MahApps.Metro.Controls.Glow' AncestorLevel =' 找到源。 BindingExpression:路徑= GlowColor;的DataItem = NULL; 目標元素是'SolidColorBrush'(HashCode = 9047482);目標 屬性是「顏色」(Type「色彩」)

XAML代碼:

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

回答

6

聽起來好像與你的全局樣式,模板或用戶控件綁定錯誤。

我寫了一些關於調試綁定錯誤here

總之,如果你在分號和逗號添加換行符,並讀取錯誤向後

做到這一點與你的錯誤,你會得到這個最簡單的方法:

 
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; 

它告訴你:

  • 某處你有一個Color屬性導致綁定錯誤。

  • 該屬性是SolidColorBrush對象

  • 該項目的DataContextnull

  • 而且結合它有問題的評估是一個RelativeSource綁定,進一步尋找MahApps.Metro.Controls.Glow對象了視覺樹,以便它可以找到該對象的GlowColor屬性,並使用它。

嘗試在您的應用程序上搜索GlowColor並查看是否可以找到它。這很可能在您的Colours.xaml文件中,因爲它可能包含您的SolidColorBrush對象。

相關問題