Awarious OfficePortal - HRMS Application

What are WPF Attached Properties and events?

+1 vote
asked Jul 7, 2013 in WPF by anonymous

What is really an attached property and attached event we have in WPF? What is the purpose of those and how do I use it? Is there any example for attached property?


1 Answer

+1 vote
answered Jul 8, 2013 by Aadhira (1,215 points)
selected Jul 9, 2013 by administrator
Best answer

Attached Properties:
Attached properties are also same as dependency property. Each control will have its own dictionary object to store resource information. That dictionary will also be used to store dependency property and attached property.Dependency property will be used in the same class and also the same class’s dictionary object will be used to store the dependency property value. But the Attached properties can be used in the child classes, also child classed resource dictionary will be used to store the attached property value.

For example,
“Background”property of the DockPanel is a dependency propery. So, this Background property can be only used with the dock panel. And, whenever you change value or retrieve value for that background property, the value will be store and retrieved from the Dictionary which is in the DockPanel class.

But, the same DockPanel has Dock property which is an Attached property. Since it is an attached property, this property can be used in its child classes, directly as “DockPanel.Dock” and whenever the value is set or get, it will be stored and retrieved from the dictionary which is in the child class, not in DockPanel.

In the below example, we have 2 checkboxes. One is dock to top and another on is dock to bottom. So, ckTop check boxes, dock value will be stored in ckTop instances dictionary. And the ckBottom check boxes dock value will be stored in ckBottom instances dictionary. So, attached properties can be used in the child classes and to store unique value.

Below XAML code may be giving you some wrong impression because you may think that, “DockPanel” is a property which is in type of “DockPanel” in CheckBox class, so we can access the “DockPanel” in the CheckBox. But that is not the case. There is no property called “DockPanel” in CheckBox class.

XAML Code:

<DockPanel Background="Red" LastChildFill="False">
        <CheckBox Content="Dock Top" Height="16" Name="ckTop" DockPanel.Dock="Top"/>
         <CheckBox Content="Dock Bottom" Height="16" Name="ckBottom" DockPanel.Dock="Bottom"/>

If we achieve the functionality in C#, then it would give better idea to understand the attached property.

The equivalent code in C# is

DockPanel.SetDock(ckTop, Dock.Top);
DockPanel.SetDock(ckBottom, Dock.Bottom);

What internally happened is, DockPanel’s attached property used to set the Dock property on the ckAttach checkbox control.

So, checkbox will be used as a Dependency object to GetDock() method and so, the dock value will be stored in CheckBoxes dictionary, instead of DockPanels dictionary. Then, DockPanel will do through each of its child elements and try to find the “Dock” value stored on those each child elements to find the respective dock value of that particular control. Main advantage is, we do not need to create “Dock” property in all the child elements which are under the Dock panel, if we have more than one control and they want to have different “Dock” value

Below is the code to create attached property with name "CustomSize"

public static int GetCustomSize(DependencyObject obj)
      return (int)obj.GetValue(CustomSizeProperty);
public static void SetCustomSize(DependencyObject obj , int value)
     obj.SetValue(CustomSizeProperty, value);

public static readonly DependencyProperty CustomSizeProperty = DependencyProperty.RegisterAttached(“CustomSize”, typeof(int), typeof(CustomShapeClass), new UIPropertyMetadata(5));

Attached Events
Attached events work in the same way as attached properties. In-fact all routed events are attached events in WPF. To understand, how the WPF events are working and how an attached event can be used, then please look at the below links

Understanding bubbling and tunneling events

Understanding Attached events

Your answer


Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2019