Silverlight (WPF) - 理解Attached Property

Silverlight (WPF) - 理解Attached Property,第1张

概述  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 m

 

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的设计思路由来。

总结

以上是内存溢出为你收集整理的Silverlight (WPF) - 理解Attached Property全部内容,希望文章能够帮你解决Silverlight (WPF) - 理解Attached Property所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1047729.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存