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