Attached Properties: An Interesting Ingredient Of XAML Cuisine

Here we are to taste the uniqueness of XAML feature which is called as ‘Attached Properties’ form a view of a rookie. We’ll break this conversation into the following parts: 

  • What is an ‘Attached Property’?
  • Why and When do we need an ‘Attached Property’?
  • Implementation of ‘Attached Property’? 

So before starting the above stated aspects I would like to present you with a scenario:

Eric Smith is a lonely octogenarian man living in countryside, having three great grandchildren named as Bruce Smith, Robin Smith and Xavier Smith. Bruce is a professional football athlete, Robin is a Dentist, whereas Xavier is a professional swimmer and they resides in different towns. Due to being old, Eric does not remembers the names of his grandchildren, but just identifies them by their profession. Eric is served by a butler name ‘Alfred’, who fulfills Eric’s all commands. On fine day, Eric buys a football and ask Alfred to deliver it to his footballer grandchild, without specifying a name. But here is twist, Alfred neither knows any of Eric’s grandchildren either. So, what Alfred is going to do now?

It’s really a simple common sense, Alfred going to call each of Eric’s grandchild’s place and ask for each of their profession respectively until he finds the grandchild Associate with football profession and will deliver the football.

So the above scenario is happened to be ‘Attached Property’ in a real world.

Let me explain: 

  • What is an ‘Attached Property’?

    Attached property simply recognizes behavior or property that are associated with an object in a structure and allow attaching of data that’s required to be associated and stored into that object. 
  • Why and When do we need an ‘Attached Property’?

    The purpose of the attached property is to access the different child elements of the structure with unique behavior such as datatype. Using attached properties parent element can access the specific targeted behavior children in a structure and access as well as manipulate their values accordingly.
  • Implementation of ‘Attached Property’:

    If the class defining attached property strictly use for other types, then the class doesn’t need to be derive the DependencyObject, although one must need to address the target parameter for accessors to be of DependencyObject. The attached property must be declared as public static readonly field of type DependencyProperty followed by return value of Register.Attached method. The field name must match the attached property name, appended with the string Property, to follow the established convention of naming the identifying fields in relation to the properties that they represent. The attached property must provide the static GetPropertyName and SetPropertyName methods as accessors for the attached property.

Get Accessor: public static valueType GetPropertyName (DependencyObject target ).

Set Accessor: public static void SetPropertyName (DependencyObject target , valueTypevalue)

So considering our above scenario, let us understand the implementation:

In above scenario we can easily state the model into programming entity world:

  • Eric’s Family: A structure grid.
  • Eric: A parent element.
  • Bruce, Robin, Xavier: children elements.
  • Footballer, Swimmer, Dentist (Profession of Bruce, Robin, Xavier): Key of Behavior of child elements.
  • Football (the present bought by Eric): A value of datatype Boolean for key (Footballer profession).
  • Alfred: Our main hero and the Interface. 

Syntax

  1. public static readonly DependencyProperty IsFootballerProperty = DependencyProperty.RegisterAttached(  
  2.   "IsFootballer",  
  3.   typeof(Boolean),  
  4.   typeof(ProfessionClassObject),  
  5.   new PropertyMetadata(“”)  
  6. );  
  7. public static void SetIsFootballerProperty(Professionclass element, boolean value)  
  8. {  
  9.     element.SetValue(IsFootballerProperty, value);  
  10. }  
  11. public static Boolean Get GetIsFootballerProperty (Professionclass element)  
  12. {  
  13.     return (Boolean)element.GetValue(IsFootballerProperty);  
  14. }  
In XAML, it could be addressed as follows:
  1. < childelement my:IsFootballerProperty.IsFootballer= “{Binding (Boolean)Value}” />   
Thus, it good to share something and I hope you folks understood as well as enjoyed it.

Thanks.