绑定资源自定义控件属性 [英] Binding a resource to a custom control property

查看:108
本文介绍了绑定资源自定义控件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个自定义的按钮显示略微褪色的文字正常,全力量的文字在鼠标悬停的MouseDown 。我在定义了两个资源 Generic.xaml 我的控制,重新present的画笔这些文字颜色:

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'}" />

第二个声明使用了一个 HSL 值转换器褪色文本颜色。

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.

推荐答案

的RelativeSource TemplatedParent 只(IIRC)具有控制模板中的意思,它是指一个属性上其上施加所述模板的控制的实例。

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 用户控件的内容不是的用户控件的模板。因此,这种结合不会考虑父用户控件作为一个可行的目标。

A UserControl's content is not the template of the UserControl. So this binding won't consider the parent UserControl as a viable target.

该错误消息是指这样的事实:一个的SolidColorBrush 不具有模板;它并不适用 System.Windows.Controls.Control ,这是(最)所有模板化UI控件的基本类型。请参见 Control.Template 有关模板的详细信息。

The error message refers to the fact that a SolidColorBrush does not have a template; it does not extend System.Windows.Controls.Control, which is the base type of (most) all templated UI controls. See Control.Template for more information about templating.

您想要做的是设置一个相对源 FindAncestor 什么。

What you want to do is set a relative source of FindAncestor.

{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}

这将走上视​​觉(或者是逻辑?)树查找键入用户控件,然后绑定对名为<$ C $的公共属性的始祖C>前景。

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 called Foreground.

然而如果的SolidColorBrush 定义为资源这是不行的。资源不是视觉的一部分(或逻辑树,或两者兼而有之?目前尚不清楚),因此一个的RelativeSource 绑定将无法走路树的祖先。

However this will NOT work if the SolidColorBrush is defined as a Resource. Resources are not part of the visual (or logical tree, or both? still not clear) and therefore a RelativeSource 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.

这篇关于绑定资源自定义控件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆