C# – WPF Error: “Cannot find governing FrameworkElement or Framework…”

ccanvasframeworkelementwpf

Im getting the following error:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or
FrameworkContentElement for target element. BindingExpression:Path=ImagePath;
DataItem=null; target element is 'VisualBrush' (HashCode=52892165); target
property is 'Visual' (type 'Visual')

I'm trying to set a Canvas type from MainWindow to my WPF Control (Named SearchTextBox) property ImagePath, so the control show it. This Canvas wraps the path of an icon. When i try and run it i cant see the icon and i get the:

System.Windows.Data Error: 2.

This is my code:

My WPF Control:

SearchTextBox.cs:

 public static readonly DependencyProperty ImagePathProperty =
            DependencyProperty.Register(
                        "ImagePath",
                        typeof(Canvas),
                        typeof(SearchTextBox));

 public Canvas ImagePath
    {
        get { return (Canvas)GetValue(ImagePathProperty); }
        set { SetValue(ImagePathProperty, value); }
    }

Generic.xaml

  <Border x:Name="PART_SearchIconBorder"
                      Grid.Column="2"
                      VerticalAlignment="Stretch"
                      HorizontalAlignment="Stretch"
                      BorderBrush="{StaticResource SearchTextBox_SearchIconBorder}">
                            <Rectangle 
                                Width="15"
                                Height="15">
                                <Rectangle.Fill>
                                    <VisualBrush Stretch="Fill"
                                        Visual="{Binding ImagePath}" />
                                </Rectangle.Fill>
                            </Rectangle>

My View:

<Controls:MetroWindow x:Class="TestUI.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:l="clr-namespace:UIControls;assembly=SearchTextBox"
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    Title="Window1" Height="423" Width="487">
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Resources/Icons.xaml" />
            <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.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

<Grid>                      //----appbar_add is the canvas 
    <l:SearchTextBox ImagePath="{StaticResource appbar_add}" Height="39" Margin="118,52,116,0" VerticalAlignment="Top" Name="m_txtTest"  />
    <TextBox Margin="118,0,116,68" Name="m_txtSearchContent" Height="65" VerticalAlignment="Bottom" />
    <TextBlock HorizontalAlignment="Left" Margin="118,0,0,139" Width="107" Text="Search content" FontSize="14" Height="20" VerticalAlignment="Bottom" />
</Grid>

Any ideas? Thanks in advance.

Best Answer

I'm assuming the content in Generic.xaml is part of the ControlTemplate for the SearchTextBox class. Since it's a control template, you need to use TemplateBinding to bind to the properties of the control that the template is applied to. So, change:

Visual="{Binding ImagePath}"

to

Visual="{TemplateBinding ImagePath}"
Related Topic