Dependency properties of WPF

Source: Internet
Author: User
Tags naming convention

Introduction

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

The main difference is, which the value of a normal. NET property was read directly from a private member in your C Lass, whereas the value of a DependencyProperty is resolved dynamically when calling the GetValue() method was Inh Erited from DependencyObject.

When you set a value of a dependency property it's 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 are the name of the property and the value are the value of your 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 th At over 90% of the properties of a UI control typically stay in its initial values. Dependency properties solve these problems by only store modified properties in the instance. The default values is stored once within the dependency property.
    • Value inheritance
      When you access a dependency property the value was resolved by using a value resolu tion strategy. If no local value is set and the dependency property navigates the logical tree until it finds a value. When you set the FontSize in the root element it applies to all textblocks below except you override the value. The
    • Change notification
      Dependency properties has 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 was also used by the databinding.

Value resolution Strategy

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

The Magic behind it

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

All types, 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 are the key defined with the dependency property.
When your access a dependency property is wrapper, it internally calls to GetValue(DependencyProperty) access the value. This method resolves the value by using a value resolution strategy, which 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 to an inherited value. If No value is found it takes the default value defined in the property metadata. This sequence was a bit simplified, but it shows the main concept.

How to create a DependencyProperty

To create a DependencyProperty, add a static field of type to your type and call to DepdencyProperty create a instance of DependencyProperty.Register() a DEP Endency property. The name of the Dependendyproperty must always end with ... property. This was a naming convention in WPF.

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

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

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

Dependency propertypublic static readonly DependencyProperty currenttimeproperty =      dependencyproperty.register   ("CurrentTime", typeof (DateTime),     typeof (Myclockcontrol), New Frameworkpropertymetadata (DateTime.Now)); . NET Property Wrapperpublic datetime currenttime{    get {return (DateTime) GetValue (currenttimeproperty);}    

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

New Frameworkpropertymetadata (DateTime.Now,                        oncurrenttimepropertychanged,                        oncoercecurrenttimeproperty),                       
Value Changed Callback

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

private static void Oncurrenttimepropertychanged (DependencyObject source,         DependencyPropertyChangedEventArgs e) {    Myclockcontrol control = source as Myclockcontrol;    DateTime time = (datetime) E.newvalue;    
Coerce Value Callback

The coerce callback allows you to adjust the value if it 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. The following example we limit the time to is in the past.

Private static object Oncoercetimeproperty (DependencyObject sender, Object data) {    if (datetime) data > DateTime. Now)    {        data = DateTime.Now;    }    
Validation Callback

The Validate callback you check if the set value is valid. If you return false, an ArgumentException would be thrown. In our example demand, then the data is an instance of a DateTime .

private static bool Onvalidatetimeproperty (object data) {    

Readonly dependencyproperties

Some dependency property of WPF controls is readonly. They is often used to report the state of a control, like the property IsMouseOver . Is does do sense to provide a setter for this value.

Maybe you ask yourself, why isn't just use a normal. NET property? One important reason is so you cannot set triggers on normal. NET propeties.

Creating A read only was similar to Creating a regular DependencyProperty. Instead of calling you DependencyProperty.Register() DependencyProperty.RegisterReadonly() . This returns a DependencyPropertyKey . This key should is 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 as a normal dependency property.

Second thing to do are registering a public dependency property, which is assigned to DependencyPropertyKey.DependencyProperty . The ReadOnly property of the is, can accessed from external.

Register the private key to set the Valueprivate static readonly DependencyPropertyKey Ismouseoverpropertykey =       Dep Endencyproperty.registerreadonly ("IsMouseOver",       typeof (BOOL), typeof (MyClass),       new   Frameworkpropertymetadata (false)); Register the public property to get the Valuepublic static readonly DependencyProperty ismouseoverproperty =       ismous   Eoverpropertykey.dependencyproperty; . NET Property Wrapperpublic int ismouseover{   get {return (bool) GetValue (Ismouseoverproperty);}   

Attached Properties

Attached properties is a special kind of dependencyproperties. They allow you to attach a value to an object, does not know anything on this value.

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

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

To set the value of the 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>    
public static readonly DependencyProperty topproperty =    dependencyproperty.registerattached ("Top",     typeof (   Double), typeof (Canvas),    new Frameworkpropertymetadata (0d,        frameworkpropertymetadataoptions.inherits)); public static void Settop (UIElement element, double value) {    element.   SetValue (topproperty, value);} public static double GetTop (UIElement element) {    

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 Overrid E The property metadata and pass an propertychangedcallback. But an much easier-on-the-is-to-get the and DependencyPropertyDescriptor hookup a callback by callingAddValueChanged()

DependencyPropertyDescriptor TEXTDESCR = dependencypropertydescriptor.    Fromproperty (Textbox.textproperty, typeof (TextBox)); if (textdescr!= null) {    textdescr.addvaluechanged (MyTextBox, delegate    {        ///ADD your propery changed logic Here ...    

How to clear a local value

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

Button1. ClearValue (Button.contentproperty);
To bind a dependent zodiac to a method in XAML:
<window x:name="Winthis"x:class="Wpfapplication1.window1"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"Title="Create a read-only Dependency property"height=" -"Width=" -"> <Grid> <Viewbox> <textblock text="{Binding elementname=winthis, path=counter}"/> </Viewbox> </Grid></Window>//File:Window.xaml.csusingSystem;usingSystem.Windows;usingSystem.Windows.Threading;namespacewpfapplication1{ Public Partial classWindow1:window { PublicWindow1 () {InitializeComponent (); DispatcherTimer Timer=NewDispatcherTimer (Timespan.fromseconds (1), Dispatcherpriority.normal,Delegate                                     {                                        intNewValue = Counter = =int. MaxValue?0: Counter +1;                                     SetValue (Counterkey, newvalue);        }, Dispatcher); }         Public intCounter {Get{return(int) GetValue (Counterkey.dependencyproperty); }        }        Private Static ReadOnlyDependencyPropertyKey Counterkey =Dependencyproperty.registerreadonly ("Counter",                                                 typeof(int),                                                 typeof(WINDOW1),NewPropertyMetadata (0)); }}

Dependency properties of WPF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.