附加属性 [英] Attached Properties

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

问题描述

我对 WPF 附加属性有点困惑.当您使用附加属性时,该附加属性只能由定义它的类读取和使用是否正确?例如,如果我想使用某个附加属性作为按钮上的悬停颜色,我是否可以从按钮的模板中获取附加属性值,并且我是否能够从按钮访问附加属性以设置胡佛颜色?

I am a little confused about WPF attached properties. When you use an attached property that attached property can only be read and used by the class that defines it correct? For example if I wanted to use some attached property as a hover color on a button, can I get the attached property value from the button's template, and will I be able access the attached property from the button to set the hoover color?

推荐答案

您阅读了吗 概述?如果没有,那就去做.

Have you read the overview? If not, do it.

附加属性,例如依赖属性,只需注册另一个可以在控件的属性字典中使用的键.您可以在任何地方设置值,也可以在任何地方检索它们,它们不受类型限制.这意味着您可能只想在按钮上设置它,但也可以在文本框上设置.

Attached properties, like dependency properties, just register another key that can be used in the properties dictionary of controls. You can set values anywhere and you can retrieve them anywhere, they are not restricted by type. This means that you may only want it to be set on Buttons but it can be set on TextBoxes too.

每个控件都有自己的属性键和值字典,附加属性允许您使用新键将值写入这些字典.由于这些字典是独立的,因此它们可以为通过静态字段属性声明设置和访问的同一属性具有不同的值.

Every control has its own dictionary of property keys and values, an attached property allows you to write a value to those dictionaries using a new key. As those dictionaries are independent they can have separate values for the same property which is set and accessed via the static field property declaration.

由于附加了这些属性,您必须通过 GetValue 获取值(因为类本身无法提供 CLR 包装器).

As those properties are attached you will have to get the values via the GetValue (as the classes cannot provide a CLR-wrapper themselves).

这篇关于附加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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