2013-04-02 14 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)。也許這不能按我想要的方式完成? 感謝您的回答。 –

相關問題