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 information 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 information 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的设计思路由来。
本文介绍了三种典型AttachProperty的应用模型,包括界面布局中常用的AttachProperty、作为子元素应用于多种父元素的内容模型及向服务报告信息的AttachProperty。通过实例解释了如何使用PropertyChangedCallback来实现附加逻辑。
63

被折叠的 条评论
为什么被折叠?



