- Most of the WPF classes derive from Dependency Object. All UI Elements are derived from Dependency Object.
- The Dependency Object class gives all the functionality of the WPF dependency property system.
- Dependency Object defines WPF Property System.
- System.Windows.DependencyObject is used to create Dependency Property.
- Dependency properties are the special property in WPF that are used to support many features in WPF.
The following are steps to create DP
1. Creating Dependency Property
Create a Property that gets inherited from dependency object
2. Register the Property, This should be public static readonly. Visual Studio 2010 gives code snippet for this (propdp). This is the property that us used to identify target property while databinding .
3. Use Property accesses' as GetValue SetValue , GetValue is used to Read the property and SetValue is used to write the property .
Below is the example for this
class Foo : DependencyObject
{
public int MyCustomProperty
{
get { return (int)GetValue(MyCustomPropertyProperty);
}
set { SetValue(MyCustomPropertyProperty, value);
}
}
//Using a DependencyProperty as the backing store for MyCustomProperty.
//This enables //animation, styling, binding, etc...//Registering the property
public static readonly DependencyProperty MyCustomPropertyProperty =
DependencyProperty.Register("MyCustomProperty", typeof(int), typeof(Foo), new UIPropertyMetadata(0));
}
While registering we need to pass the parameters as Name, Type, Type that owns property and the metadata that contains property .
Meta Data Contains Default Value of properties information.
Coming up Attached Properties till then Happy BeyondRelationaling !!!