将资源绑定到自定义控件属性
我正在创建一个自定义按钮,该按钮通常显示稍微褪色的文本,并在 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')
我不确定数据错误
告诉我什么。谁能告诉我发生了什么事以及如何解决它?感谢您的帮助。
I am creating a custom button that shows slightly faded text normally, and full-strength text on a MouseOver
or MouseDown
. I have defined two resources in the Generic.xaml
of my control to represent the brushes for these text colors:
<!-- Text Brushes -->
<SolidColorBrush x:Key="NormalTextBrush" Color="Black" />
<SolidColorBrush x:Key="FadedTextBrush" Color="Gray" />
The control compiles and works fine in that configuration.
But I want to let the control user set the text color, using the custom control's Foreground
property. So, I changed the resource declarations to this:
<!-- 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'}" />
The second declaration uses an HSL
value converter to fade the text color.
Now the control doesn't work, and I get the following error in the output window:
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')
I'm not sure what the Data Error
is telling me. Can anyone tell me what's going on and how to fix it? Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅
RelativeSource TemplatedParent
(IIRC) 在控件模板中有意义,它指的是应用该模板的控件实例的属性。UserControl
的内容不是UserControl
的模板。因此,此绑定不会将父UserControl
视为可行的目标。该错误消息是指
SolidColorBrush
没有模板;它不扩展 System.Windows.Controls.Control,这是(大多数)所有模板化 UI 控件的基本类型。请参阅Control.Template
有关模板的更多信息。您想要做的是设置
FindAncestor
的相对源。这将沿着视觉(或者逻辑?)树查找
UserControl
类型的第一个祖先,然后绑定到名为Foreground
的公共属性。但是如果
SolidColorBrush
被定义为Resource
,这将不起作用。资源不是视觉(或逻辑树,或两者?仍然不清楚)的一部分,因此RelativeSource
绑定将无法遍历树的祖先。您必须直接在任何您希望与
UserControl
具有相同前景色的控件上使用绑定。RelativeSource TemplatedParent
only (IIRC) has meaning within a control template, and it refers to a property on the instance of the control on which the template is applied.A
UserControl
's content is not the template of theUserControl
. So this binding won't consider the parentUserControl
as a viable target.The error message refers to the fact that a
SolidColorBrush
does not have a template; it does not extendSystem.Windows.Controls.Control
, which is the base type of (most) all templated UI controls. SeeControl.Template
for more information about templating.What you want to do is set a relative source of
FindAncestor
.This will walk up the visual (or is it logical?) tree to find the first ancestor of type
UserControl
, then bind against a public property calledForeground
.However this will NOT work if the
SolidColorBrush
is defined as aResource
. Resources are not part of the visual (or logical tree, or both? still not clear) and therefore aRelativeSource
binding will not be able to walk the tree's ancestry.You will have to use the binding directly on whatever control you wish to have the same foreground color as the
UserControl
.问题是您不能对资源中定义的元素使用
RelativeSource
绑定,因为它们不是可视树或逻辑树的一部分。要解决此问题,您只需在设置资源引用的位置(在按钮的控件模板中)设置这些绑定。类似这样:
换句话说,您不需要定义资源 -
NormalTextBrush
和FadedTextBrush
。The problem is that you cannot use
RelativeSource
bindings on elements defined in resources, because they are not a part of visual or logical tree.To fix this you just need to set these binding in places where you set the references to your resources (in the control template of your button). Something like this:
In other words, you don't need to define resourses -
NormalTextBrush
andFadedTextBrush
.