【问题标题】:WPF Error: "Cannot find governing FrameworkElement or Framework..."WPF 错误:“找不到管理 FrameworkElement 或 Framework...”
【发布时间】:2013-03-24 06:42:41
【问题描述】:

我收到以下错误:

System.Windows.Data 错误:2:找不到管理 FrameworkElement 或 目标元素的 FrameworkContentElement。绑定表达式:路径=图像路径; 数据项=空;目标元素是“VisualBrush”(HashCode=52892165);目标 属性是“视觉”(类型“视觉”)

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

System.Windows.Data 错误:2。

这是我的代码:

我的 WPF 控件:

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>

我的观点:

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

有什么想法吗?提前致谢。

【问题讨论】:

    标签: c# wpf canvas frameworkelement


    【解决方案1】:

    我假设 Generic.xaml 中的内容是 SearchTextBox 类的 ControlTemplate 的一部分。由于它是一个控件模板,因此您需要使用TemplateBinding 绑定到应用该模板的控件的属性。所以,改变:

    Visual="{Binding ImagePath}"
    

    Visual="{TemplateBinding ImagePath}"
    

    【讨论】:

    • 嗨,是的,这解决了我遇到的错误,但仍然看不到图标/图像:/。我使用的图标来自 nuget 包 MahApps.Metro.Resources (mahapps.com/MahApps.Metro/#icons)。也许这不能按照我想要的方式完成?谢谢你的回答。
    猜你喜欢
    • 2014-02-03
    • 2021-08-22
    • 2020-02-10
    • 2013-08-17
    • 2011-03-12
    • 2016-06-02
    • 2013-08-22
    • 2011-12-17
    相关资源
    最近更新 更多