in Education by
What is WPF Dependency Property and how can we use?

1 Answer

0 votes
by
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.
 
WPF Dependency Property 
 
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
  1. public class CarDependencyProperty: DependencyObject  
  2. {  
  3.     //Register Dependency Property  
  4.     public static readonly DependencyProperty CarDependency = DependencyProperty.Register("MyProperty"typeof (string), typeof (DependencyPropertySample));  
  5.     public string MyCar  
  6.     {  
  7.         get  
  8.         {  
  9.             return (string) GetValue(CarDependency);  
  10.         }  
  11.         set  
  12.         {  
  13.             SetValue(CarDependency, value);  
  14.         }  
  15.     }  
  16. }  
  17. public partial class CarDependencyPropertyDemo: Window  
  18. {  
  19.     public CarDependencyPropertyDemo()  
  20.     {  
  21.         InitializeComponent();  
  22.     }  
  23.     private void MyButton_Click(object sender, RoutedEventArgs e)  
  24.     {  
  25.         CarDependency dpSample = TryFindResource("CarDependency "as CarDependency;  
  26.         MessageBox.Show(dpSample.MyCar);  
  27.     }  
  28. }  

Related questions

0 votes
0 votes
    What is XAML in WPF and also explain the types of XAML?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
    What is a WPF Child Window?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
0 votes
0 votes
    What are Resources in WPF?...
asked Apr 8, 2021 in Education by JackTerrance
...