2

我收到以下错误:

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

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

System.Windows.Data 错误:2。

这是我的代码:

我的 WPF 控件:

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

通用的.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>

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

4

1 回答 1

1

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

Visual="{Binding ImagePath}"

Visual="{TemplateBinding ImagePath}"
于 2013-04-03T00:15:54.630 回答