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

时间:2013-04-02 19:51:57

标签: c# wpf canvas frameworkelement

我收到以下错误:

  

System.Windows.Data错误:2:找不到管理FrameworkElement或     目标元素的FrameworkContentElement。 BindingExpression:路径=的ImagePath;     的DataItem = NULL; target元素是'VisualBrush'(HashCode = 52892165);目标     属性是'Visual'(类型'Visual')

我正在尝试将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>

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

1 个答案:

答案 0 :(得分:1)

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

Visual="{Binding ImagePath}"

Visual="{TemplateBinding ImagePath}"