Understanding Default values for Dependency Properties

Understanding Windows Presentation Foundation Dependency Properties is a bit confusing when first learning WPF. Learn how to tap into the power of Dependency Properties.

Understanding WPF Dependency Properties is a bit confusing when first learning WPF. The confusion arises if you assume that Dependency Property's work the same as C# or Visual Basic properties. Once you understand that Dependency Properties don't use local storage for the Property backing data, but instead entrust that responsibility to the Dependency System you are on your way to learning the Zen of WPF. To tap into the power of Dependency...

Properties you have to register your property with the Dependency System.

Once registered, as shown above, you have a property called HighlightBrush which you can use in your XAML.

Default Values
Providing a default value for a.NET property makes sense in certain circumstances and it is reasonable feature to have in WPF Dependency Property's too. So let's learn a little more about how default values work in WPF Dependency Properties.

Find more resources on Dependency Properties

WPF dependency properties: WPF reflections 

Additional information on Windows Presentation Foundation 

For this example we'll look at the ProgressBar.Maximum property. It has a default value of 100, which I found by spending a few minutes probing the WPF API with the RedGate Reflector tool (http://www.red-gate.com/products/reflector/).

Here is a simplified version of the registration code for the Maximum property.


At runtime, the Dependency System uses an evaluation algorithm to determine what the current value is for a Dependency Property. The default value is returned, if no other item is affecting the property. Plenty of other factors can influence what the Dependency System returns. For example a running animation, active data-binding or a WPF style could alter the current value for the Maximum property.

Let's look at more XAML; this example creates a few ProgressBars.

In the C# we can check the value.

Determining the original default
If the default value has been change directly, by modifying the property in XAML or setting the property in code, you have dig a little to determine the original default value.

Here are some examples.


This was first published in April 2010

Dig deeper on Windows Presentation Foundation

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close