WPF 错误:“找不到管理 FrameworkElement 或 Framework...”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15773128/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:25:59  来源:igfitidea点击:

WPF Error: "Cannot find governing FrameworkElement or Framework..."

c#wpfcanvasframeworkelement

提问by Gustavo Calderon

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

System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。BindingExpression:Path=ImagePath; 数据项=空;目标元素是“VisualBrush”(HashCode=52892165);目标属性是“Visual”(类型“Visual”)

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

我正在尝试将Canvas类型设置MainWindow为我的WPF Control(Named SearchTextBox) 属性ImagePath,以便控件显示它。这Canvas包装了一个图标的路径。当我尝试运行它时,我看不到图标,我得到:

System.Windows.Data Error: 2.

System.Windows.Data 错误:2。

This is my code:

这是我的代码:

My WPF Control:

我的 WPF 控件:

SearchTextBox.cs:

搜索文本框.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.

有任何想法吗?提前致谢。

回答by Flangus

I'm assuming the content in Generic.xaml is part of the ControlTemplatefor the SearchTextBoxclass. Since it's a control template, you need to use TemplateBindingto bind to the properties of the control that the template is applied to. So, change:

我假设在Generic.xaml内容的部分ControlTemplateSearchTextBox类。既然是控件模板,就需要使用TemplateBinding来绑定到应用模板的控件的属性上。所以,改变:

Visual="{Binding ImagePath}"

to

Visual="{TemplateBinding ImagePath}"