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:

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:

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:

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.