Introduction
Dependency Properties are a type of property that extend out a CLR property. Whereas a CLR property is read directly from a member of your class, a Dependency Property will be dynamically resolved when calling the GetValue() method that your object gains via inheritance from the base DependencyObject class.
This section will break down Dependency Properties and explain their usage both conceptually and through code examples.
Syntax
- DependencyProperty.Register(string name, Type propertyType, Type ownerType)
- DependencyProperty.Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata)
- DependencyProperty.Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata, ValidateValueCallback validateValueCallback)
- DependencyProperty.RegisterAttached(string name, Type propertyType, Type ownerType)
- DependencyProperty.RegisterAttached(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata)
- DependencyProperty.RegisterAttached(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata, ValidateValueCallback validateValueCallback)
- DependencyProperty.RegisterReadOnly(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata)
- DependencyProperty.RegisterReadOnly(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata, ValidateValueCallback validateValueCallback)
- DependencyProperty.RegisterAttachedReadOnly(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata)
- DependencyProperty.RegisterAttachedReadOnly(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata, ValidateValueCallback validateValueCallback)
Parameters
Parameter | Details |
---|
name | The String representation of the property's name |
propertyType | The Type of the property, e.g. typeof(int) |
ownerType | The Type of the class in which the property is being defined, e.g. typeof(MyControl) or typeof(MyAttachedProperties) . |
typeMetadata | Instance of System.Windows.PropertyMetadata (or one of its subclasses) that defines default values, property changed callbacks, FrameworkPropertyMetadata allows for defining binding options like System.Windows.Data.BindingMode.TwoWay . |
validateValueCallback | Custom callback that returns true if the property's new value is valid, otherwise false. |