【转】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 ...
随机推荐
- HTML知识点总结以及典型例子讲解
一.HTML文本格式化标签(这些标签都不换行) eg: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...
- UILabel 添加图片
//设置显示图片 NSMutableAttributedString * cellAttributeStr = [[NSMutableAttributedString alloc]initWithSt ...
- Swift-05-SizeOf&&SizeOfValue
在C中定义的运算符,可以作用于类型或者某个实际的变量,并返回其在内存中的尺寸size_t. 在Cocoa中,我们一般用来将数组生成NSData的时候传入数据长度.因为在OC里面,sizeOf这个C运算 ...
- Sersync实现触发式文件同步 替代inotify和rsync
Sersync实现触发式文件同步 替代inotify和rsync Pyinotify是一个Python模块,用来监测文件系统的变化. Pyinotify依赖于Linux内核的功能—inotify(内核 ...
- 端口转发后执行putty连接------------------》VirtualBox+ubuntu_server
login as: fleam fleam@127.0.0.1's password: Welcome to Ubuntu LTS (GNU/Linux --generic i686) * Docum ...
- Java代码中执行Linux命令,亲测可用
前提需要知道怎么在linux怎么新建java文件和怎么编译,否则请先学其他知识!! import java.io.*;public class Test{ public static void mai ...
- JavaScript权威指南(第六版)--JavaScript概述 DEMO
//所有在双斜线之后的内容都属于注释 //仔细阅读这里的注释:它们对JavaScript代码做了解释 //变量是表示值的一个符号名字 //变量是通过var关键字声明的 var x; //声明一个变量x ...
- 第二篇 SQL Server代理作业步骤和子系统
本篇文章是SQL Server代理系列的第二篇,详细内容请参考原文. SQL Server代理作业由一系列的一个或多个作业步骤组成.一个作业步骤分配给一个特定的作业子系统(确定作业步骤去完成的工作). ...
- javascript设计模式学习之七——迭代器模式
一.迭代器模式定义 迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,并且不需要暴露该对象的内部表示. 在当前大部分语言中,都已经内置了迭代器模式.迭代器有内部迭代器和外部迭代器之分,一般现有语 ...
- iOS开发——XML/JSON数据解析
NSJSONSerialization 接下来就正式开始.苹果官方给出的解析方式是性能最优越的,虽然用起来稍显复杂. 首先我们在上面已经有了我希望得到的信息的网站的API给我们的URL,在OC中,我要 ...