Search code examples
wpfdependency-propertiesattached-properties

WPF/Attached Properties - Please explain why this works


Please help me understand where the value "ABC" gets stored. When I run memory profilers I don't see any instance of MyClass, and in fact the binding works and the GroupBox.Header gets the value ABC...
Thanks for your help.

<GroupBox Header="{Binding Path=(local:MyClass.Tag1), RelativeSource={RelativeSource Self}}"  
          local:MyClass.Tag1="ABC" />
public class MyClass
{
    public static readonly DependencyProperty Tag1Property = DependencyProperty.RegisterAttached("Tag1", typeof(object), typeof(MyClass), new UIPropertyMetadata(null));
    public static object GetTag1(DependencyObject obj)
    {
        return obj.GetValue(Tag1Property);
    }
    public static void SetTag1(DependencyObject obj, object value)
    {
        obj.SetValue(Tag1Property, value);
    }
}

Solution

  • Dependency properties maintain a dictionary internally. Values are stored using sparse storage mechanism. These properties are associated at the class level - being static. The value ABC is stored in the dictionary as key value pairs