Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
• 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.
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
call DependencyProperty.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.
// Dependency Property
public static readonly DependencyProperty CurrentTimeProperty =
DependencyProperty.Register( "CurrentTime", typeof(DateTime),
typeof(MyClockControl), new
FrameworkPropertyMetadata(DateTime.Now));
Each DependencyProperty provides callbacks for change notification, value coercion and
validation. These callbacks are registered on the dependency property.
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.
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.
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.
Readonly DependencyProperties
Some dependency property of WPF controls are readonly. They are often used to report the state
of a control, like the IsMouseOver 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.
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:
<Canvas>
<Button Canvas.Top="20" Canvas.Left="20" Content="Click me!"/>
</Canvas>
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 the DependencyPropertyDescriptor and hookup a callback by
calling AddValueChanged()
if (textDescr!= null)
{
textDescr.AddValueChanged(myTextBox, delegate
{
// Add your propery changed logic here...
});
}
button1.ClearValue( Button.ContentProperty );