Introduction

Value resolution strategy

The magic behind it

How to create a DepdencyProperty

Readonly DependencyProperties

Attached DependencyProperties

Listen to dependency property changes

How to clear a local value

Introduction

When you begin to develop appliations with WPF, you will soon stumble across DependencyProperties. They look quite similar to normal .NET properties, but the concept behind is much more complex and powerful.

The main difference is, that the value of a normal .NET property is read directly from a private member in your class, whereas the value of a DependencyProperty is resolved dynamically when calling the GetValue() method that is inherited from DependencyObject.

When you set a value of a dependency property it is not stored in a field of your object, but in a dictionary of keys and values provided by the base class DependencyObject. The key of an entry is the name of the property and the value is the value you want to set.

The advantages of dependency properties are

  • Reduced memory footprint
    It's a huge dissipation to store a field for each property when you think that over 90% of the properties of a UI control typically stay at its initial values. Dependency properties solve these problems by only store modified properties in the instance. The default values are stored once within the dependency property.
  • Value inheritance
    When you access a dependency property the value is resolved by using a value resolution strategy. If no local value is set, the dependency property navigates up the logical tree until it finds a value. When you set the FontSize on the root element it applies to all textblocks below except you override the value.
  • Change notification
    Dependency properties have a built-in change notification mechanism. By registering a callback in the property metadata you get notified, when the value of the property has been changed. This is also used by the databinding.

Value resolution strategy

Every time you access a dependency property, it internally resolves the value by following the precedence from high to low. It checks if a local value is available, if not if a custom style trigger is active,... and continues until it founds a value. At last the default value is always available.


The magic behind it

Each WPF control registers a set of DependencyProperties to the static DependencyProperty class. Each of them consists of a key - that must be unique per type - and a metadata that contain callbacks and a default value.

All types that want to use DependencyProperties must derive from DependencyObject. This baseclass defines a key, value dictionary that contains local values of dependency properties. The key of an entry is the key defined with the dependency property.
When you access a dependency property over its .NET property wrapper, it internally calls GetValue(DependencyProperty) to access the value. This method resolves the value by using a value resolution strategy that is explained in detail below. If a local value is available, it reads it directly from the dictionary. If no value is set if goes up the logical tree and searches for an inherited value. If no value is found it takes the default value defined in the property metadata. This sequence is a bit simplified, but it shows the main concept.


How to create a DependencyProperty

To create a DependencyProperty, add a static field of type DepdencyProperty to your type and callDependencyProperty.Register() to create an instance of a dependency property. The name of the DependendyProperty must always end with ...Property. This is a naming convention in WPF.

To make it accessable as a normal .NET property you need to add a property wrapper. This wrapper does nothing else than internally getting and setting the value by using the GetValue() and SetValue() Methods inherited from DependencyObject and passing the DependencyProperty as key.

Important: Do not add any logic to these properties, because they are only called when you set the property from code. If you set the property from XAML the SetValue() method is called directly.

If you are using Visual Studio, you can type propdp and hit 2x tab to create a dependency property.

  1.   // Dependency Property
  2. public static readonly DependencyProperty CurrentTimeProperty =
  3. DependencyProperty.Register( "CurrentTime", typeof(DateTime),
  4. typeof(MyClockControl), new FrameworkPropertyMetadata(DateTime.Now));   // .NET Property wrapper
  5. public DateTime CurrentTime
  6. {
  7. get { return (DateTime)GetValue(CurrentTimeProperty); }
  8. set { SetValue(CurrentTimeProperty, value); }
  9. }   

Each DependencyProperty provides callbacks for change notification, value coercion and validation. These callbacks are registered on the dependency property.

  1.   new FrameworkPropertyMetadata( DateTime.Now,
  2. OnCurrentTimePropertyChanged,
  3. OnCoerceCurrentTimeProperty ),
  4. OnValidateCurrentTimeProperty );   
Value Changed Callback

The change notification callback is a static method, that is called everytime when the value of the TimeProperty changes. The new value is passed in the EventArgs, the object on which the value changed is passed as the source.

  1.   private static void OnCurrentTimePropertyChanged(DependencyObject source,
  2. DependencyPropertyChangedEventArgs e)
  3. {
  4. MyClockControl control = source as MyClockControl;
  5. DateTime time = (DateTime)e.NewValue;
  6. // Put some update logic here...
  7. }   
Coerce Value Callback

The coerce callback allows you to adjust the value if its outside the boundaries without throwing an exception. A good example is a progress bar with a Value set below the Minimum or above the Maximum. In this case we can coerce the value within the allowed boundaries. In the following example we limit the time to be in the past.

  1.   private static object OnCoerceTimeProperty( DependencyObject sender, object data )
  2. {
  3. if ((DateTime)data > DateTime.Now )
  4. {
  5. data = DateTime.Now;
  6. }
  7. return data;
  8. }   
Validation Callback

In the validate callback you check if the set value is valid. If you return false, an ArgumentException will be thrown. In our example demand, that the data is an instance of a DateTime.

  1.   private static bool OnValidateTimeProperty(object data)
  2. {
  3. return data is DateTime;
  4. }   

Readonly DependencyProperties

Some dependency property of WPF controls are readonly. They are often used to report the state of a control, like theIsMouseOver property. Is does not make sense to provide a setter for this value.

Maybe you ask yourself, why not just use a normal .NET property? One important reason is that you cannot set triggers on normal .NET propeties.

Creating a read only property is similar to creating a regular DependencyProperty. Instead of callingDependencyProperty.Register() you call DependencyProperty.RegisterReadonly(). This returns you aDependencyPropertyKey. This key should be stored in a private or protected static readonly field of your class. The key gives you access to set the value from within your class and use it like a normal dependency property.

Second thing to do is registering a public dependency property that is assigned toDependencyPropertyKey.DependencyProperty. This property is the readonly property that can be accessed from external.

  1.   // Register the private key to set the value
  2. private static readonly DependencyPropertyKey IsMouseOverPropertyKey =
  3. DependencyProperty.RegisterReadOnly("IsMouseOver",
  4. typeof(bool), typeof(MyClass),
  5. new FrameworkPropertyMetadata(false));   // Register the public property to get the value
  6. public static readonly DependencyProperty IsMouseoverProperty =
  7. IsMouseOverPropertyKey.DependencyProperty;   // .NET Property wrapper
  8. public int IsMouseOver
  9. {
  10. get { return (bool)GetValue(IsMouseoverProperty); }
  11. private set { SetValue(IsMouseOverPropertyKey, value); }
  12. }   

Attached Properties

Attached properties are a special kind of DependencyProperties. They allow you to attach a value to an object that does not know anything about this value.

A good example for this concept are layout panels. Each layout panel needs different data to align its child elements. The Canvas needs Top and Left, The DockPanel needs Dock, etc. Since you can write your own layout panel, the list is infinite. So you see, it's not possible to have all those properties on all WPF controls.

The solution are attached properties. They are defined by the control that needs the data from another control in a specific context. For example an element that is aligned by a parent layout panel.

To set the value of an attached property, add an attribute in XAML with a prefix of the element that provides the attached property. To set the the Canvas.Top and Canvas.Left property of a button aligned within a Canvas panel, you write it like this:

  1. <Canvas>
  2. <Button Canvas.Top="20" Canvas.Left="20" Content="Click me!"/>
  3. </Canvas> 
  1.   public static readonly DependencyProperty TopProperty =
  2. DependencyProperty.RegisterAttached("Top",
  3. typeof(double), typeof(Canvas),
  4. new FrameworkPropertyMetadata(0d,
  5. FrameworkPropertyMetadataOptions.Inherits));   public static void SetTop(UIElement element, double value)
  6. {
  7. element.SetValue(TopProperty, value);
  8. }   public static double GetTop(UIElement element)
  9. {
  10. return (double)element.GetValue(TopProperty);
  11. }   

Listen to dependency property changes

If you want to listen to changes of a dependency property, you can subclass the type that defines the property and override the property metadata and pass an PropertyChangedCallback. But an much easier way is to get theDependencyPropertyDescriptor and hookup a callback by calling AddValueChanged()

  1.   DependencyPropertyDescriptor textDescr = DependencyPropertyDescriptor.
  2. FromProperty(TextBox.TextProperty, typeof(TextBox));   if (textDescr!= null)
  3. {
  4. textDescr.AddValueChanged(myTextBox, delegate
  5. {
  6. // Add your propery changed logic here...
  7. });
  8. }   

How to clear a local value

Because null is also a valid local value, there is the constant DependencyProperty.UnsetValue that describes an unset value.

  1. button1.ClearValue( Button.ContentProperty );

Dependency Properties的更多相关文章

  1. Silverlight:《Pro Silverlight5》读书笔记 之 Dependency Properties And Routed Event

    Dependency Properties And Routed Event Dependency Properties Dynamic Value Resolution As you’ve alre ...

  2. [WPF系列]基础 Listening to Dependency Property change notifications of a given Element

    I want to share this great post from Anoop that shows a easy way to add a notification system to dep ...

  3. Maven - dependency那些事儿

    身边有几位刚使用Maven的同学表示——在一个叫"pom.xml"的文件里声明一个依赖就不用去手动添加jar了,感觉这东西和自己手动管理依赖没太大区别. 当然,并不是这样,在此记录 ...

  4. spring boot properties文件与yaml文件的区别

    编写是没有提示的话在pom中添加依赖,如下: <!-- 配置文件处理器 编写配置时会有提示 --> <dependency> <groupId>org.spring ...

  5. 使控件具有 Tilt 效果

    步骤1:添加类: /* Copyright (c) 2010 Microsoft Corporation. All rights reserved. Use of this sample source ...

  6. WPF 虚拟化 VirtualizingWrapPanel 和 VirtualLizingTilePanel

    一. UI  上两个扩展 public class VirtualizingWrapPanel : VirtualizingPanel, IScrollInfo { #region Fields UI ...

  7. Springmvc responsebody 返回对象属性 是date日期格式时 如何返回给前台自己想要的形式

    1添加依赖 <!-- Jackson Json处理工具包 -->            <dependency>              <groupId>org ...

  8. [WPF系列]-基础系列 Property Trigger, DataTrigger & EventTrigger

    So far, we worked with styles by setting a static value for a specific property. However, using trig ...

  9. spring mvc redis消息队列

    通常情况下,为了提高系统开发的灵活性和可维护度,我们会采用消息队列队系统进行解耦.下面是一个采用spring redis实现的消息队列实例,但此实例会由于网络延迟和阻塞等情况导致消息处理的延时,因而不 ...

随机推荐

  1. 检查C++内存泄露

    #ifdef _DEBUG #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__) #else #define DEBUG_ ...

  2. iOS 十六进制和字符串转换

    NSString *dictString = [dict JSONFragment];//组合成的. dictString==={"content":"Sadgfdfg& ...

  3. 4.2 set和multiset

    使用必须包含头文件set 1)multiset *:定义 如果不给第二个参数,默认less<key>,即用<来进行. 例如: A是一个类的名字,则可以定义一个容器对象如下: mult ...

  4. MongoDB的介绍和使用场景(1)

    MongoDB 是一个高性能,开源,无模式的文档型数据库,是当前 NoSQL 数据库产品中最热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式,MongoDB 使用 C++开发.M ...

  5. HDU4495 Rectangle

    求组成的等腰三角形面积最大值. 对此题的总结:暴力出奇迹 组成的三角形放置方式一共只有4种,用ans表示目前已知的最长三角形的边长,从上到下,从左到右枚举顶点,再枚举边长,一个重要剪枝是枚举边长l时先 ...

  6. 在Salesforce中以PDF的格式显示对应的页面

    在Salesforce中可以简单设置page的属性让页面以pdf的方式显示内容, 当然了我们的page内容可以用Html的方式编写 设置方式为:renderAs="pdf" 请看如 ...

  7. 在Salesforce中编写Unit Test

    Unit Test 也是一个 Class 文件,所以在创建 Unit Test 的时候要选择 Test Class 类型来创建,请看如下截图(在Eclipse中): 编写 Unit Test 基本流程 ...

  8. javase基础笔记1——简介和发展

    软件分为 系统软件 windows linux类 (unix)mac(麦金塔).数据库管理系统 unix linux 开源os(open source) 免费 开放 free os operation ...

  9. apache服务器安装

    下载地址:http://www.apachehaus.com/cgi-bin/download.plx 全程按这篇来的,很顺利 http://www.cnblogs.com/yerenyuan/p/5 ...

  10. MFC 丢失MSVCR120D.dll 丢失mfc120ud.dll