WPF has provided some extended services to the CLR property that we can collectively call Dependency Properties. A Dependency Property is a property whose value depends on the external sources, such as animation, data binding, styles, or visual tree inheritance. Not only this, but a Dependency Property also has the builtin feature of providing notification when the property has changed, data binding and styling.
Advantages of a Dependency Property:
- Less memory consumption
- Property value inheritance
- Change notification and Data Bindings
- Participation in animation, styles and templates
- CallBacks
- Resources
- Overriding Metadata
Code
- public class CarDependencyProperty: DependencyObject
- {
-
- public static readonly DependencyProperty CarDependency = DependencyProperty.Register("MyProperty", typeof (string), typeof (DependencyPropertySample));
- public string MyCar
- {
- get
- {
- return (string) GetValue(CarDependency);
- }
- set
- {
- SetValue(CarDependency, value);
- }
- }
- }
- public partial class CarDependencyPropertyDemo: Window
- {
- public CarDependencyPropertyDemo()
- {
- InitializeComponent();
- }
- private void MyButton_Click(object sender, RoutedEventArgs e)
- {
- CarDependency dpSample = TryFindResource("CarDependency ") as CarDependency;
- MessageBox.Show(dpSample.MyCar);
- }
- }