Silverlight (WPF) - 理解Attached Property

110 篇文章 0 订阅
90 篇文章 0 订阅

 

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

 

 


 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值