Attached Property Overview 中提到了三种典型的Attach Property的应用模型:
How Attached Properties Are Used by the Owning Type
Although attached properties are settable on any object,that does not automatically mean that setting the property will produce a tangible result,or that the value will ever be used by another object. Generally,attached properties are intended so that objects coming from a wide variety of possible class hierarchies or logical relationships can each report common @R_989_4045@ion to the owning type. The type that defines the attached property for Silverlight typically follows one of these models:
1. The type that defines the attached property is designed so that it can be the parent element of the elements that will set values for the attached property. The type then iterates its child elements through internal logic,obtains the values,and acts on those values in some manner.
2. The type that defines the attached property will be used as the child element for a variety of possible parent elements and content models.
3. The attached property reports @R_989_4045@ion to a service.
第一种就是最常见的用于界面布局的attach property,例如Canvas.Top,DockPanel.Dock等
第二种暂没有遇到
第三种没具体解释,不好理解。Nathan的blog提到,这种情况其实就是Attached Service 或者叫 Attached Behavior 。可以理解为,就是把一些attached property的owner中的逻辑attach到其他对象中去。ToolTipService.ToolTip就是一个最直观的例子。Nathan还给了一个链接 ,也是一个很简单明了的Attached Service的例子,将鼠标右键及其事件处理逻辑抽象为一个Attached Service,可attach到其他任何适用的对象中去。
BenCon给出了另外一个attached service的例子 ,他称之为Ramora Pattern (应该是Remora,一种有吸盘的鱼,用来比喻attached service 可以吸附到其他对象上)
总结一下两个例子,Attached Service是利用了PropertyChangedCallback回调函数来注册目标对象(被attached的对象)上的某个事件,并处理事件来实现其附加逻辑的。
Nick的2005年的一篇老文 ,讲述了Attached Property的设计思路由来。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。