Posted by: Zeeshan Amjad | September 17, 2009

Callback function when value of Dependency Property Change


We have already seen that we can assign additional information with dependency property such as default value, Coerce function and callback function with the help of PropertyMetaData class. With the help of that class we can control each and every dependency property individually. We can even change the value of individual dependency property with the help of those call back functions.

But sometimes we just need the notification when the value of any dependency property changes. In that case we are more interested in the new and old value rather than enforcing any rule on it. In addition, we want to get the notification of all the dependency properties of our class.  DependencyObject class provide one call back method named OnPropertyChanged exactly for this purpose. This call back function calls every time when there is a value change of any dependency property. Lets take a look at the class diagram of DependencyObject.

DependencyObject

This class has one call back function named OnPropertyChanged. This function takes only one parameter named DependencyPropertyChangedEventArgs. This class has three read only properties. These properties will give information about which dependency property changes, what was its old value and what is its new value. Here is a class diagram of this class.

DependencyPropertyChangedEventArgs

Now lets make a simple program to demonstrate this concept. Let’s take our old example of Employee class with only two dependency properties. .

  1: protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  2: {
  3:     Message += e.Property + " Dependency Property Changed";
  4:     Message += "\nOld Value = " + e.OldValue + "\nNew Value = " + e.NewValue;
  5:     Message += "\n-----------------------\n";
  6: 
  7:     base.OnPropertyChanged(e);
  8: }

Here Message is normal CLR property to store all the changes, which we are going to display latter in our main window. Here is a complete coding of this simple program.

  1: using System;
  2: using System.Windows;
  3: 
  4: public class Employee : DependencyObject
  5: {
  6:     public String Message
  7:     { get; set; }
  8: 
  9:     public static readonly DependencyProperty NameProperty =
 10:         DependencyProperty.Register("Name", typeof(string), typeof(Employee));
 11: 
 12:     public string Name
 13:     {
 14:         get { return (string)GetValue(NameProperty); }
 15:         set { SetValue(NameProperty, value); }
 16:     }
 17: 
 18: 
 19:     public static readonly DependencyProperty CompanyProperty =
 20:         DependencyProperty.Register("Company", typeof(string), typeof(Employee));
 21: 
 22:     public string Company
 23:     {
 24:         get { return (string)GetValue(CompanyProperty); }
 25:         set { SetValue(CompanyProperty, value); }
 26:     }
 27: 
 28:     protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
 29:     {
 30:         Message += e.Property + " Dependency Property Changed";
 31:         Message += "\nOld Value = " + e.OldValue + "\nNew Value = " + e.NewValue;
 32:         Message += "\n-----------------------\n";
 33: 
 34:         base.OnPropertyChanged(e);
 35:     }
 36: }
 37: 
 38: public class ReadOnlyDPSample
 39: {
 40:     [STAThread]
 41:     public static void Main()
 42:     {
 43:         Window win = new Window();
 44:         win.Title = "DependencyObject OnPropertyChanged";
 45:         win.Width = 400;
 46:         win.Height = 300;
 47: 
 48:         Employee emp = new Employee();
 49: 
 50:         emp.Name = "Bob Smith";
 51:         emp.Company = "WPF In Corp";
 52: 
 53:         // now change the company
 54:         emp.Company = "New WPF In Corp";
 55: 
 56:         String text = String.Format("Emp ID = {0}\nName = {1}",
 57:             emp.Name, emp.Company);
 58: 
 59:         win.Content = emp.Message;
 60: 
 61:         Application app = new Application();
 62:         app.Run(win);
 63: 
 64:     }
 65: }

Here is the output of this program.

OnPropertyChanged

Advertisements

Responses

  1. […] saw one example of defining the callback function when the value of dependency property changed here. But there is one major difference between these two […]


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: