Posted by: Zeeshan Amjad | January 28, 2010

Clear the value of dependency property


If we want to clear the value of dependency property then we will use the Clear Value function. There are multiple advantages of it. For example animation works only on dependency property, and if for some reason we want to change the behavior of animation at run time therefore we will use this function. One more application of this method is in data binding. If we want to remove the binding at run time then we will use this.

ClearValue is a method in DependencyObject class. Here is a class diagram of DependencyObject.

DependencyObjectClass

Here is one simple program to demonstrate this concept.

  1: using System;
  2: using System.Windows;
  3: 
  4: public class Employee : DependencyObject
  5: {
  6:     public static readonly DependencyProperty EmpIdProperty =
  7:         DependencyProperty.Register("EmpId", typeof(int), typeof(Employee));
  8: 
  9:     public int EmpId
 10:     {
 11:         get { return (int)GetValue(EmpIdProperty); }
 12:         set { SetValue(EmpIdProperty, value); }
 13:     }
 14: 
 15:     public static readonly DependencyProperty NameProperty =
 16:         DependencyProperty.Register("Name", typeof(string), typeof(Employee));
 17: 
 18:     public string Name
 19:     {
 20:         get { return (string)GetValue(NameProperty); }
 21:         set { SetValue(NameProperty, value); }
 22:     }
 23: 
 24:     public static readonly DependencyProperty CompanyProperty =
 25:         DependencyProperty.Register("Company", typeof(string), typeof(Employee),
 26:         new PropertyMetadata("WPF In Corp"));
 27: 
 28:     public string Company
 29:     {
 30:         get { return (string)GetValue(CompanyProperty); }
 31:         set { SetValue(CompanyProperty, value); }
 32:     }
 33: }
 34: 
 35: public class DependencyPropertySample
 36: {
 37:     [STAThread]
 38:     public static void Main()
 39:     {
 40:         Window win = new Window();
 41:         win.Title = "Dependency Property";
 42:         win.Width = 300;
 43:         win.Height = 200;
 44: 
 45:         Employee emp = new Employee();
 46: 
 47:         emp.EmpId = 1000;
 48:         emp.Name = "Bob Smith";
 49: 
 50:         emp.ClearValue(Employee.EmpIdProperty);
 51:         emp.ClearValue(Employee.NameProperty);
 52:         emp.ClearValue(Employee.CompanyProperty);
 53: 
 54:         PropertyMetadata pmd = Employee.CompanyProperty.GetMetadata(emp);
 55: 
 56: 
 57:         String text = String.Format("Emp ID = {0}\nName = {1}\nCompany = {2}\nDefault Value = {3}",
 58:             emp.EmpId, emp.Name, emp.Company, pmd.DefaultValue);
 59: 
 60:         win.Content = text;
 61: 
 62:         Application app = new Application();
 63:         app.Run(win);
 64:     }
 65: }
 66: 

We set the value of dependency property of two properties and one dependency property has default value. Here is the output of this program.

DependencyProperty_ClearValue

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: