C# – Cannot find source for binding with reference ‘RelativeSource FindAncestor, AncestorType=’MahApps.Metro.Controls.Glow’, AncestorLevel=’1”

cfindancestorrelativesourcewpfxaml

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.

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

xaml code:

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

Best Answer

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 Color property causing a binding error.

  • That property is on a SolidColorBrush object

  • The DataContext of that item is null.

  • And the binding it's having problems evaluating is a RelativeSource binding that is looking for a MahApps.Metro.Controls.Glow object further up the visual tree so it can find the GlowColor property of that object and use it.

Try doing a search on your application for GlowColor and see if you can find it. It's most likely in your Colours.xaml file, since that probably contains your SolidColorBrush objects.