闲话WPF之九(Dependency属性 [1] )

WPF引入了一种新的属性:Dependency属性。Dependency属性的应用贯串在整个WPF当中。Dependency属性根据多个提供对象来决定它的值。并且是及时更新的。提供对象可以是动画,不断地改变它的值。也可以是父元素,它的属性值被继承到子元素。毫无疑问,Dependency属性最大的特点就是内建的变化通知功能。提供Dependency属性功能主要是为了直接从声明标记提供丰富的功能。WPF声明的友好设计的关键是大量的使用属性。如果没有Dependency属性,我们将不得不编写大量的代码。关于WPF的Dependency属性,我们将重点研究如下三个方面:

1、变化通知功能:属性的值被改变后,通知界面进行更新。

2、属性值的继承功能:子元素将继承父元素中对应属性名的值。

3、支持多个提供对象:我们可以通过多种方式来设置Dependency属性的值。

先看如何实现一个标准的Dependency属性。

public class Button : ButtonBase

{

   // The dependency property

   public static readonly DependencyProperty IsDefaultProperty;

   static Button()

   {

      // Register the property

      Button.IsDefaultProperty = DependencyProperty.Register(“IsDefault”,  typeof(bool), typeof(Button),

            new FrameworkPropertyMetadata(false,

            new PropertyChangedCallback(OnIsDefaultChanged)));

            …

   }

   // A .NET property wrapper (optional)

   public bool IsDefault

   {

         get { return (bool)GetValue(Button.IsDefaultProperty); }

         set { SetValue(Button.IsDefaultProperty, value); }

   }

   // A property changed callback (optional)

   private static void OnIsDefaultChanged(

   DependencyObject o, DependencyPropertyChangedEventArgs e) { … }

   …

}

在上面的实现代码中,System.Windows.DependencyProperty类表示的静态字段IsDefaultProperty才是真正的Dependency属性。为了方便,所有的Dependency属性都是公有、静态的,并且还有属性后缀。通常创建Dependency属性可用静态方法DependencyProperty.Register。参数的属性名称、类型、使用这个属性的类。并且可以根据重载的方法提供其他的通知事件处理和默认值等等。这些相关的信息可参考FrameworkPropertyMetadata类的多个重载构造函数。

最后,实现了一个.NET属性,其中调用了从System.Windows.DependencyObject继承的GetValue、SetValue方法。所有具有Dependency属性的类都肯定会继承这个类。GetValue方法返回最后一次设置的属性值,如果还没有调用一次SetValue,返回的将是Register方法所注册的默认值。而且,这种.NET样式的属性封装是可选的,因为GetValue/SetValue方法本身是公有的。我们可以直接调用这两个函数,但是这样的封装使代码更可读。

虽然这种实现方式比较麻烦,但是,由于GetValue/SetValue方法使用了一种高效的小型存储系统,以及真正的Dependency属性是静态字段(不是实例字段),Dependency属性的这种实现可以大大的减少每个属性实例的存储空间。想象一下,如果Button50个属性,并且全部是非静态的实例字段,那么每个Button实例都含有这样50个属性的空间,这就存在很大的空间浪费。除了节省空间,Dependency属性的实现还集中、并且标准化了属性的线程访问检查、提示元素重新提交等等。
posted @ 2006-12-23 16:07 Yiling Lai 阅读(3775) 评论(7)  编辑 收藏 网摘 所属分类: WPF

  回复  引用    
#1楼 2006-12-23 20:47 | 锦瑟 [未注册用户]
请问现在哪里有中文wpf开发资源,教程之类的?
  回复  引用  查看    
#2楼 [楼主]2006-12-23 21:30 | Yiling Lai      
@锦瑟

很抱歉,我现在还没有找到任何正式的中文资源。也许不久会有中文版的书籍上市。
  回复  引用    
#3楼 2007-01-21 03:15 | namhyuk [未注册用户]
One of the primary architectural philosophies used in building WPF was a preference for proerties over methods or events. Properties are declarative and allow you to more easily specify intent instead of action. This also supported a model driven, or data driven, system for displaying user interface content. This philosophy had the intended effect of creating more properties that you could bind to, in order to better control the behavior of an application.

In order to have more of the system driven by properties, a richer property system than what the CLR provides was needed. A simple example of this richness is change notifications. In order to enable two way data binding, you need both sides of the bind to support change notification. In order to have behavior tied to property values, you need to be notifed when the property value changes. The Microsoft .NET Framework has an interface, INotifiyPropertyChanged, which allows an object to publish change notifications, however it is optional.

WPF provides a richer property system, derived from the DependencyObject type. The property system is truly a "dependency" property system in that it tracks dependencies between property expressions and automatically revalidates property values when dependencies change. For example, if you have a property that inherits (like FontSize), the system is automatically updated if the property changes on a parent of an element that inherits the value.

The foundation of the WPF property system is the concept of a property expression. In this first release of WPF, the property expression system is closed, and the expressions are all provided as part of the framework. Expressions are why the property system doesn't have data binding, styling, or inheritance hard coded, but rather provided by later layers within the framework.

The property system also provides for sparse storage of property values. Because objects can have dozens(if not hundreds) of properties, and most of the values are in their default state(inherited, set by styles, etc.), not every instance of an object needs to have the full weight of every property defined on it.

The final new feature of the property system is the notion of attached properties. WPF elements are built on the principle of composition and component reuse. It is often the case some containing element(like a Grid layout element) needs additional data on child elements to control its behavior(like the Row/Column information). Instead of associating all of these properties with every element, any object is allowed to provide property definitions for any other object. This is similar to the "expando" features of JavaScript.
  回复  引用    
#4楼 2007-03-23 17:46 | dansinge [未注册用户]
加油
  回复  引用  查看    
#5楼 [楼主]2007-03-24 16:15 | Yiling Lai      
@ dansinge

谢谢!




标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2006-12-29 09:01 编辑过
Google站内搜索

China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》

相关文章:

相关链接: