Dependency property in WPF

Rama Sagar
Posted by in WPF category on for Beginner level | Points: 250 | Views : 3209 red flag

A property that is backed by the WPF property system is known as a dependency property.

Introduction

This article explains about the dependency property in WPF

Dependency properties are properties of classes that derive from DependencyObject, and they're special in that rather than simply using a backing field to store their value, they use some helper methods on DependencyObject


How to create a DependencyProperty?

Add a static field of type DepdencyProperty to our type and call DependencyProperty.Register() to create an instance of a dependency property. The name of the DependendyProperty must always end with ...Property which is a naming convention in WPF.

Inorder to make it accessable as a normal .NET property we 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.

// Dependency Property
public static readonly DependencyProperty PresentTimeProperty = 
     DependencyProperty.Register( "PresentTime", typeof(DateTime),
     typeof(MyControl), new FrameworkPropertyMetadata(DateTime.Now));
 
// .NET Property wrapper
public DateTime CurrentTime
{
    get { return (DateTime)GetValue(PresentTimeProperty); }
    set { SetValue(PresentTimeProperty, value); }
}

Readonly DependencyProperties

There are dependency property like read only wpf controls which are often used to report the state of a control, like the IsMouseOver property. Creating a read only property is similar to creating a regular DependencyProperty. Instead of calling DependencyProperty.Register() we will call DependencyProperty.RegisterReadonly(). This returns us a DependencyPropertyKey. This key should be stored in a private or protected static readonly field of your class. The key gives us access to set the value from within our class and use it like a normal dependency property.

// Register the private key to set the value
private static readonly DependencyPropertyKey IsMouseOverPropertyKey = 
      DependencyProperty.RegisterReadOnly("IsMouseOver", 
      typeof(bool), typeof(sampleclass), 
      new FrameworkPropertyMetadata(false));
 
// Register the public property to get the value
public static readonly DependencyProperty IsMouseoverProperty = 
      IsMouseOverPropertyKey.DependencyProperty;    
 
// .NET Property wrapper
public int IsMouseOver
{
   get { return (bool)GetValue(IsMouseoverProperty); }
   private set { SetValue(IsMouseOverPropertyKey, value); }
}


Conclusion
In this we try to learn about dependency property in WPF

Reference

http://msdn.microsoft.com/en-us/library/ms752914.aspx

Page copy protected against web site content infringement by Copyscape

About the Author

Rama Sagar
Full Name: RamaSagar Pulidindi
Member Level: Silver
Member Status: Member,MVP
Member Since: 12/30/2012 1:51:40 AM
Country: India
ramasagar
http://www.ramasagar.com
A Software Profesional working in Microsoft .NET technologies since year 2008, and I work for Dake ACE. I am passionate about .NET technology and love to contribute to the .NET community at Dot Net Funda

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)