gpt4 book ai didi

c# - WPF 错误 : "Cannot find governing FrameworkElement or Framework..."

转载 作者:太空宇宙 更新时间:2023-11-03 13:44:40 25 4
gpt4 key购买 nike

我收到以下错误:

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 控件:

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

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

最佳答案

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

Visual="{Binding ImagePath}"

Visual="{TemplateBinding ImagePath}"

关于c# - WPF 错误 : "Cannot find governing FrameworkElement or Framework...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15773128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com