A Dependency property is a property which allow the analytical code to modify the properties of an object to minimize a data necessities by giving a powerful reporting system about to change of a data in a particular way.
In dot net generally we have a two types of properties:
1. Normal property
2. DependencyProperty
In this blog we discuss the Dependency property.
To implement the Dependency Property we need to implement a UserControl class from a INotifyPropertyChanged interface first.
public partial class UserControlDependencyProperty : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropChange;
protected void OnPropertyChanged(string propName)
{
if (PropChange!= null)
{
PropChange(this, new PropertyChangedEventArgs(propName));
}
}
}
Now create a normal property with name "Triangle"
public string Triangle
{
get { return GetValue(TriangleProperty).ToString(); }
set { SetValue(TriangleProperty, value); }
}
After creating normal property we need to pass a property field that is used to store the data in above code as a parameter to a Register method to register the DependencyProperty in the CLR.
public static readonly DependencyProperty TriangleProperty =
DependencyProperty.Register("Triangle", typeof(string), typeof(UserControlDependencyProperty),
new PropertyMetadata(string.Empty, OnTrianglePropertyChanged));
Code for OnTrianglePropertyChanged event handler:
private static void OnTrianglePropertyChanged(DependencyObject dependObject,
DependencyPropertyChangedEventArgs e)
{
UserControlDependencyProperty userControl = dependObject as UserControlDependencyProperty ;
UserControlDependencyProperty.OnPropertyChanged("Triangle");
UserControlDependencyProperty.OnTrianglePropertyChanged(e);
}
private void OnTrianglePropertyChanged(DependencyPropertyChangedEventArgs e)
{
txt1.Text = Triangle;
}
Code for XAML
<local:UserControlDependencyProperty triangle=" This is the Dependency Property Example" />
0 Comment(s)