【转】The Attached Behavior Pattern
原文:http://www.bjoernrochel.de/2009/08/19/the-attached-behavior-pattern/
The Attached Behavior Pattern
AUG 19TH, 2009
With new technologies often completely new patterns emerge as people try to check-out how far a technology might take them. One interesting example for this is the Attached Behavior Pattern which seems to get a lot of attention in the WPF and Silverlight community lately. It’s a great example of what you can do with those technologies that was previously only very hard to achieve (although sometimes in my calm moments it feels a little bit like a hack to me).
Anyway, here is what the pattern actually does:
The Attached Behavior Pattern uses the moment when an attached DependencyProperty is attached to a DependencyObject in order to wire event handlers to it. With this you’re able to extend the behavior of elements in the WPF trees with arbitrary code.
Let’s take a look at how this can be achieved. First of all we need a class deriving from DependencyObject in order to be able to provide an attached DependencyProperty. It uses the standard pattern of registering the IsEnabled DependencyProperty in the static constructor (which is a bit clunky, btw).
A behavior must derive from DependencyObject
1 |
|
Pretty standard stuff so far. However you can use another constructor of FrameworkPropertyMetdata in order to supply a callback which is called when the value of the attached property changes.
Registering for change notifications
1 |
|
Next thing we need to do is to configure this attached property on the DependencyObject we want to attach behavior to. This is preferably done in XAML. You’ve got at least two options for doing the configuration. The first option is to set your attached property in the XAML file of a UserControl, Page or Window directly at the source, the element you want to attach behavior to.
Wiring it via XAML
1 |
|
The other option is to use the the power of styles (an area were WPF really shines imho) in order to set it for all elements to which a style is applied.
Wiring it via XAML and styles
1 |
|
Personally I very much vote for the second option, because it enables you to specify the attached behavior in a single place in contrast to configuring it at several places in your solution. Don’t repeat yourself, unless you really need to.
Now, fasten your seatbelts. This is were the fun begins. When the WPF / Silverlight infrastructure loads the compiled baml from the resources and builds the element tree from it (and with this attaches our new property to the specified elements) the callback method gets called.
Attaching to the target instance
1 |
|
With first parameter of the callback you get a reference to the DependencyObject to which our attached property has been attached to. You’re now able to wire all sort of event handlers to the DependencyObject and are able to execute arbitrary code when the event occurs.
Here ends our journey for today. Attached Behaviors is a really nice technique which enabled me to do something that I always wanted to have in my WinForms apps but never found a satisfiing way to implement it (just a bit patience, I’m going to blog about it soon). I think it will be interesting to see how this pattern is used in the future. While new patterns always have the tendency to be overused, I think that this particular pattern can enable lots of valuable things …
【转】The Attached Behavior Pattern的更多相关文章
- 年度巨献-WPF项目开发过程中WPF小知识点汇总(原创+摘抄)
WPF中Style的使用 Styel在英文中解释为”样式“,在Web开发中,css为层叠样式表,自从.net3.0推出WPF以来,WPF也有样式一说,通过设置样式,使其WPF控件外观更加美化同时减少了 ...
- 【WPF系列】基础学习-WPF设计模式概览
引言 “设计模式”这个让程序员提起来就“酷”的东东,着实让让初学设计模式的programer自我陶醉一番.太多的经历,告诉我们“凡事都要个度,要学会适可而止”,否则过犹不及.“设计模式”也一样,切莫为 ...
- Yii源码阅读笔记(七)
接上次的组件(component)代码: /** * Returns a list of behaviors that this component should behave as. * 定义该对象 ...
- Yii源码阅读笔记(六)
组件(component),是Yii框架的基类,实现了属性.事件.行为三类功能,如果需要事件和行为的功能,需要继承该类,不需要可直接继承Object类: namespace yii\base; use ...
- wpf 面试题目
初级工程师 解释什么是依赖属性,它和以前的属性有什么不同?为什么在WPF会使用它?什么是样式什么是模板绑定(Binding )的基础用法解释这几个类的作用及关系: Visual, UIElement, ...
- yii2源码学习笔记(四)
继续了解组件Component.php /** * Returns a value indicating whether a property is defined for this componen ...
- yii2源码学习笔记(三)
组件(component),是Yii框架的基类,实现了属性.事件.行为三类功能,如果需要事件和行为的功能,需要继承该类. yii\base\Component代码详解 <?php /** * @ ...
- Yii创建前台和后台登录表单和通过扩展 CWebUser 增加信息到 Yii::app()->user
我参考了这篇文章来构建项目的前台和后台的目录结构.感谢Andy的这篇文章.按照所有的步骤,您将有单独的前台和后台面板,如: http://localhost/index.php // 前台 http: ...
- A WPF File ListView and ComboBox
源码下载: Download FileListView_Version_2.zip Download FileListView_Version_2_Binaries.zip Download File ...
随机推荐
- 关于带透明度的灰度层的show、hide
原理图如下:[需要注意的是,灰度View与中间的小View是并列的关系,否则,带透明度的灰度图就会影响小View的透明度] - (void)show{ UIWindow *win = [[UIAppl ...
- Windows-009-Win7 操作系统安装图文详解
此文主要讲述 Win7 操作系统的安装,配以详细的图文介绍,希望能对亲有所帮助,若有不足之处,敬请大神指正,不胜感激! 若是亲在虚拟机(VirtualBox)中安装 Win7,其前期虚拟硬件系统的操作 ...
- 学习WEb前端开发的需要哪些条件
第一阶段--HTML的学习. 超文本标记语言(HyperText Mark-up Language 简称HTML)是一个网页的骨架,无论是静态网页还是动态网页,最终返回到浏览器端的都是HTML代码,浏 ...
- iOS:app直播---采集篇
[如何快速的开发一个完整的iOS直播app](采集篇) 转载自简书@袁峥Seemygo:http://www.jianshu.com/p/c71bfda055fa 前言 开发一款直播app,首先需要采 ...
- POJ 1041问题描述
Description Little Johnny has got a new car. He decided to drive around the town to visit his friend ...
- 智能硬件+App移动新生态【10.24北京站】
活动概况 时间:2015年10月24日13:30-16:30 地点:Wepac空间(海淀区北四环西路68号左岸工社6层) 主办:APICloud.机智云.智石科技.华为云 网址:www.apiclou ...
- 一些Demo链接
youtube下载神器:https://github.com/rg3/youtube-dl我擦咧vim插件:https://github.com/Valloric/YouCompleteMevim插件 ...
- Python模块(scapy)
scapy scapy相当于linux的tcpdump的功能
- linux操作技巧
1. 打开终端 ctrl+ALT+T 新终端 ctrl+shift+T 原有终端新页面 2. 忘记root密码 redhat下 单用户进入grub 在核心文件后加"single&qu ...
- box2dweb之关节joint(连接器)
1 概述 前篇基础文章看完后基本上就应该对box2dweb能上手了,下面来介绍一下box2dweb非常重要的一个概念,关节(joint)也有叫连接器的,总之是一个意思.下面是关节详细的类库说明: BO ...