Posted by: Zeeshan Amjad | September 15, 2009

Getting the dependency property detail


We can get the detail of dependency property with the help of DependencyPropertyHelper class. This class is inherited by Object and has only one static method named GetValueSource. The return type of GetValueSource is a structure ValueSource. Here is a class diagram of DependencyPropertyHelper class.

DependencyPropertyHelper

The value source structure contains four properties, but the most important one is the BaseValueSource property. This property report the source that provide the value of the dependency property. Here is the class diagram of this structure.

ValueSource

Here BaseValueSource is enumeration type and it has these values.

  1:     // Summary:
  2:     //     Identifies the property system source of a particular dependency property
  3:     //     value.
  4:     public enum BaseValueSource
  5:     {
  6:         // Summary:
  7:         //     Source is not known. This is the default value.
  8:         Unknown = 0,
  9:         //
 10:         // Summary:
 11:         //     Source is the default value, as defined by property metadata.
 12:         Default = 1,
 13:         //
 14:         // Summary:
 15:         //     Source is a value through property value inheritance.
 16:         Inherited = 2,
 17:         //
 18:         // Summary:
 19:         //     Source is from a setter in the default style. The default style comes from
 20:         //     the current theme.
 21:         DefaultStyle = 3,
 22:         //
 23:         // Summary:
 24:         //     Source is from a trigger in the default style. The default style comes from
 25:         //     the current theme.
 26:         DefaultStyleTrigger = 4,
 27:         //
 28:         // Summary:
 29:         //     Source is from a style setter of a non-theme style.
 30:         Style = 5,
 31:         //
 32:         // Summary:
 33:         //     Source is a trigger-based value in a template that is from a non-theme style.
 34:         TemplateTrigger = 6,
 35:         //
 36:         // Summary:
 37:         //     Source is a trigger-based value of a non-theme style.
 38:         StyleTrigger = 7,
 39:         //
 40:         // Summary:
 41:         //     Source is an implicit style reference (style was based on detected type or
 42:         //     based type). This value is only returned for the Style property itself, not
 43:         //     for properties that are set through setters or triggers of such a style.
 44:         ImplicitStyleReference = 8,
 45:         //
 46:         // Summary:
 47:         //     Source is based on a parent template being used by an element.
 48:         ParentTemplate = 9,
 49:         //
 50:         // Summary:
 51:         //     Source is a trigger-based value from a parent template that created the element.
 52:         ParentTemplateTrigger = 10,
 53:         //
 54:         // Summary:
 55:         //     Source is a locally set value.
 56:         Local = 11,
 57:     }
 58: 

Whenever there is a default value of dependency property and we are not setting its value then we will get the Default as a result. And whenever the value of dependency property is set by SetValue function of DependencyObject then the result will be local.

Let’s take a look at our previous sample of Employee class and try to get the information about dependencies properties of this class. Here is a simple C# program to demonstrate this concept.

  1: using System;
  2: using System.Windows;
  3: 
  4: public class Employee : DependencyObject
  5: {
  6:     public static readonly DependencyProperty NameProperty =
  7:         DependencyProperty.Register("Name", typeof(string), typeof(Employee));
  8: 
  9:     public string Name
 10:     {
 11:         get { return (string)GetValue(NameProperty); }
 12:         set { SetValue(NameProperty, value); }
 13:     }
 14: 
 15:     private static readonly DependencyPropertyKey CompanyPropertyKey =
 16:         DependencyProperty.RegisterReadOnly("Company", typeof(string), typeof(Employee),
 17:         new PropertyMetadata("WPF In Corp"));
 18: 
 19:     public static readonly DependencyProperty CompanyProperty = 
 20:         CompanyPropertyKey.DependencyProperty;
 21: 
 22:     public string Company
 23:     {
 24:         get { return (string)GetValue(CompanyProperty); }
 25:         private set { SetValue(CompanyProperty, value); }
 26:     }
 27: }
 28: 
 29: public class ReadOnlyDPSample
 30: {
 31:     [STAThread]
 32:     public static void Main()
 33:     {
 34:         Window win = new Window();
 35:         win.Title = "DependencyPropertyHelper";
 36:         win.Width = 400;
 37:         win.Height = 300;
 38: 
 39:         Employee emp = new Employee();
 40: 
 41:         emp.Name = "Bob Smith";
 42: 
 43:         ValueSource vsName = DependencyPropertyHelper.GetValueSource(emp, Employee.NameProperty);
 44:         ValueSource vsCompany = DependencyPropertyHelper.GetValueSource(emp, Employee.CompanyProperty);
 45: 
 46:         String text = String.Format("Emp ID = {0}\nName = {1}",
 47:             vsName.BaseValueSource.ToString(), vsCompany.BaseValueSource.ToString());
 48: 
 49:         win.Content = text;
 50: 
 51:         Application app = new Application();
 52:         app.Run(win);
 53: 
 54:     }
 55: }
 56: 

Here is the output of this program.

DependencyPropertyHelperOutput

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: