2013-04-02 74 views
2

即时得到以下错误:WPF错误:“找不到理事FrameworkElement的或框架......”

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

我试图从MainWindow一个Canvas类型设置为我WPF Control(命名SearchTextBox)财产ImagePath ,所以控制显示它。这Canvas包装图标的路径。当我尝试运行它,我不能看到图标和我得到的:

System.Windows.Data Error: 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> 

任何想法?提前致谢。

回答

1

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

Visual="{Binding ImagePath}" 

Visual="{TemplateBinding ImagePath}" 
+0

你好,是的,这解决了错误香港专业教育学院一直有,但仍然无法看到图标/图片:/。我使用的图标来自nuget包MahApps.Metro.Resources(http://mahapps.com/MahApps.Metro/#icons)。也许这不能按我想要的方式完成? 感谢您的回答。 –