A new type of property, called a dependency property, was added to the .Net Framework with the release of .Net 3.0. Dependency properties are used in both Windows Presentation Foundation (WPF) and Workflow Foundations (WF). They are very similar in both frameworks, but are used for different purposes.
Dependency properties provides the plumbing for property value resolution, change notification, data binding, styling, validation, etc. for properties exposed in Windows Presentation Foundation (WPF) UI elements and Workflow Foundations (WF) custom activities. Each dependency property is registered with a central repository that handles the change event notifications for you.
Key Point – The Value of Dependency Properties Are Resolved
The ultimate goal of a dependency property, like any property, is to manage state. But unlike normal .Net properties, the local property value is not stored in an instance variable.
Instead, dependency properties are registered with the dependency property framework, and the underlying property value is resolved – meaning the value is determined by the dependency property framework based on rules defined by the property registration.
How To Create A Dependency Property
All WPF UI elements and Workflow Activities are derived from a high-level base classes called DependencyObject, which provides the basic functionality required to implement dependency properties.
public class MySampleControl : Control
{
// Step 1: Register the dependency property
public static readonly DependencyProperty SpecialBrushProperty =
DependencyProperty.Register("SpecialBrush", typeof(Brush),
typeof(MySampleControl));
// Step 2: Provide set/get accessors for the property
public Brush SpecialBrush
{
// IMPORTANT!!
// -----------
// Dependency property accessors should not include custom logic
// because the framework may call the base.GetValue() and
// SetValue() methods directly
get { return (Brush)base.GetValue(SpecialBrushProperty); }
set { base.SetValue(SpecialBrushProperty, value); }
}
}
As show in the code, there are two steps involved in creating a dependency property:
- Create a static field to hold a DependencyProperty object
- By convention, the field should be named with the normal property name, followed by a “Property” suffix
- This field will not contain the value of the property. It simply defines the property that is registered with the dependency system
- This field should be defined as a public, static, read-only field
- The property must be available at all times, possibly shared among classes
- The field is defined with the read-only keyword, meaning it can only be set in the static constructor of the type.
- The field must be instantiated with a call to the static DependencyPropert.Register() method, passing:
- The name of the property
- The type of the property
- The type of the class that owns the property
- Optionally, metadata used to define how the property is treated by the WPF framework
- Meta-data flags to specify how the property affects the layout of the element (AffectsMeasure, AffectsArrange, AffectsRender, etc.)
- Callbacks for handling property value changes (PropertyChangedCallback)
- Callbacks to define custom value logic (CoerceValueCallback)
- Callbacks to validate values (ValidateValueCallback)
- Optionally, metadata used to define how the property is treated by the WF framework
- Include DependencyPropertyOptions enumerated value to specify additional characteristics for the property (ReadOnly, MetaData, etc.)
- Create property accessors (get / set)
- Call DependencyProperty.SetValue() and GetValue() methods to set local values
- DependencyObject is a high-level framework base class for WPF UI elements and WF activities
- DependencyObject exposes the GetValue() and SetValue() methods
Isn’t That A Lot of Code for a Property?
At first glance, yes, this looks like an extremely complicated way to declare a property. OK, at second glance, it’s still pretty complex. But once you understand the power of dependency properties provide, especially in the Windows Presentation Framework (WPF), they’ll become a welcome addition to your coding toolbox.
The convention takes a little getting used to. But after you’ve created a few, it comes quite naturally. Visual Studio also includes some snippets to help add dependency properties:
- Insert Snippet > NetFX30 > Define a DependencyProperty
- Insert Snippet > Other > Workflow > DependencyProperty > Property
Subtle Differences Between in WPF and WF
While WPF and WF both use a similar dependency property frameworks, they are not identical!!
WPF UI elements and WF activities derive from a base DependencyObject class, and use a DependencyProperty class to register properties and get/set property values. Despite the shared names, they are not the same classes!!
- DependencyObject and DependencyProperty classes used by WPF reside in the System.Windows namespace
- DependencyObject and DependencyProperty classes used by WF reside in the System.Workflow.ComponentModel namespace
Though their usage and function are very similar, they are not directly related to one another.
How are Dependency Property Values Resolved?
Property values for dependency properties are resolved automatically by the framework according to the following order of precedence:
- Property system coercion
- Active animations or animations with a hold behavior (WPF)
- Local value
- TemplateParent template properties (WPF)
- Implicit style (WPF)
- Style triggers (WPF)
- Template triggers (WPF)
- Style setters (WPF)
- Default (theme) style (WPF)
- Inheritance
- Default value from dependency property metadata
Note that the local value is 3rd in the order of precedence. This means that a property may have a value, even if a local value for the property has never been set. Also, if a property does have a local value, it may be overridden by higher level precedence items like an animation or data binding.
When Should I Use Dependency Properties In WPF?
Dependency properties are one of the most important concepts within WPF. In WPF, dependency properties are required to support:
- Data Binding
- Animation
- Styling
- Value expressions
- Property invalidation
- Per-type default values
In short, it makes sense to expose dependency properties any time you expose a property for a custom UI element or control.
When Should I Use Dependency Properties In WF?
Dependency properties are not used as heavily in Workflow Foundations (WF) as they are in Windows Presentation Foundation (WPF) , but they are still an important part of the WF framework. They are used primarily when developing custom workflow activities that expose properties to support the following scenarios:
- Activity Binding
- This is the most common use of dependency properties in WF
- Allows activities to expose properties that can be used to bind state to another activity
- For example, binding the input of one workflow activity to the output of another workflow activity
- Attached Properties
- Attached properties are used to create a parent activity that can manage the state of it’s child activities
- For example, the Conditioned Activity Group can attach a When property to each child
- Meta Properties
- Meta properties are defined by including new PropertyMetadata(DependencyPropertyOptions.Metadata) in the DependencyProperty.Register() method call
- Meta properties are set at design time and cannot be changed at run-time
- Meta properties exist to guarantee the integrity of an activity
- For example, at runtime, you can’t change the InterfaceType of an CallExternalMethod activity because InterfaceType is a meta-property
Sources