Posted by: Zeeshan Amjad | September 24, 2009

Attached Property in WPF


Attached property is used as a global dependency property that can be set on any object. Typical example of this type of dependency property are some properties of panel inherited classes such as Left, Right, Top, Bottom properties of Canvas class, or Dock property of DockPanel class.

Even at the code level attached property is different than dependency property. Unlike dependency properties, there are no CLR property wrapper for attached properties. The reason behind is it that attached property can be set with another namespace using XAML.

Lets take a look at our previous example of Employee class and make its Company property as a attached property. Here is a sample code to make Employee class.

  1: public class Employee : DependencyObject
  2: {
  3:     public static readonly DependencyProperty NameProperty =
  4:         DependencyProperty.Register("Name", typeof(string), typeof(Employee));
  5: 
  6:     public static readonly DependencyProperty CompanyProperty =
  7:         DependencyProperty.RegisterAttached("Company", typeof(string), typeof(Employee));
  8: 
  9:     public string Name
 10:     {
 11:         get { return (string)GetValue(NameProperty); }
 12:         set { SetValue(NameProperty, value); }
 13:     }
 14: 
 15:     public static void SetCompany(UIElement element, string value)
 16:     {
 17:         element.SetValue(CompanyProperty, value);
 18:     }
 19: 
 20:     public static string GetCompany(UIElement element)
 21:     {
 22:         return (string)element.GetValue(CompanyProperty);
 23:     }
 24: }

 

Here we have two different functions for the attached property rather than normal CLR wrapper. Even its usage is different than dependency property. Here is a sample code for using attached property.

  1: Employee emp = new Employee();
  2: emp.Name = "Bob Smith";
  3: emp.SetValue(Employee.CompanyProperty, "WPF In Corp");
  4: 

 

Here is a complete 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 static readonly DependencyProperty CompanyProperty =
 10:         DependencyProperty.RegisterAttached("Company", typeof(string), typeof(Employee));
 11: 
 12:     public string Name
 13:     {
 14:         get { return (string)GetValue(NameProperty); }
 15:         set { SetValue(NameProperty, value); }
 16:     }
 17: 
 18:     public static void SetCompany(UIElement element, string value)
 19:     {
 20:         element.SetValue(CompanyProperty, value);
 21:     }
 22: 
 23:     public static string GetCompany(UIElement element)
 24:     {
 25:         return (string)element.GetValue(CompanyProperty);
 26:     }
 27: }
 28: 
 29: public class MyWindow : Window
 30: {
 31:     public MyWindow()
 32:     {
 33:         Title = "Attached Property";
 34:         Width = 400;
 35:         Height = 300;
 36:     }
 37: }
 38: 
 39: public class AttachedProperty
 40: {
 41:     [STAThread]
 42:     public static void Main()
 43:     {
 44:         MyWindow win = new MyWindow();
 45: 
 46:         Employee emp = new Employee();
 47:         emp.Name = "Bob Smith";
 48:         emp.SetValue(Employee.CompanyProperty, "WPF In Corp");
 49: 
 50:         String text = String.Format("Name = {0}\nCompany = {1}",
 51:             emp.Name, emp.GetValue(Employee.CompanyProperty));
 52: 
 53:         win.Content = text;
 54: 
 55:         Application app = new Application();
 56:         app.Run(win);
 57:     }
 58: }
 59: 

 

Here is the output of this program

 

AttachedProperty

Advertisements

Responses

  1. […] and define Attached dependency property in it. We already saw the attached properties here. We have to write as many attached dependency properties as many events we want to handle. In our […]

  2. […] Dependency Property We saw the example of dependency property here. Now we are going to use attached dependency property to modify the controls behavior. Therefore […]


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: