【发布时间】:2011-06-22 21:59:35
【问题描述】:
我正在创建一个自定义按钮,该按钮通常显示略微褪色的文本,并在 MouseOver 或 MouseDown 上显示完整的文本。我在控件的Generic.xaml 中定义了两个资源来表示这些文本颜色的画笔:
<!-- Text Brushes -->
<SolidColorBrush x:Key="NormalTextBrush" Color="Black" />
<SolidColorBrush x:Key="FadedTextBrush" Color="Gray" />
该控件在该配置中编译并正常工作。
但我想让控件用户设置文本颜色,使用自定义控件的Foreground 属性。因此,我将资源声明更改为:
<!-- Text Brushes -->
<SolidColorBrush x:Key="NormalTextBrush" Color="{Binding Path=Foreground, RelativeSource={RelativeSource TemplatedParent}}" />
<SolidColorBrush x:Key="FadedTextBrush" Color="{Binding Path=Foreground, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource ColorConverter}, ConverterParameter='1.2'}" />
第二个声明使用HSL 值转换器来淡化文本颜色。
现在控件不起作用,我在输出窗口中收到以下错误:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Foreground; DataItem='TaskButton' (Name='Button1'); target element is 'SolidColorBrush' (HashCode=38118303); target property is 'Color' (type 'Color')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Foreground; DataItem=null; target element is 'SolidColorBrush' (HashCode=47449297); target property is 'Color' (type 'Color')
我不确定Data Error 告诉我什么。谁能告诉我发生了什么以及如何解决它?感谢您的帮助。
【问题讨论】:
标签: wpf wpf-controls binding