WPF / Attached Properties - Please explain why this works.

Please help me understand where the value of "ABC" is stored. When I start the memory profilers, I do not see any instances of MyClass, and in fact the bindings and GroupBox.Header get 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);
    }
}
+3
source share
2 answers

Dependency properties support an internal dictionary. Values ​​are stored using a sparse storage engine. These properties are class-level related - static. The ABC value is stored in the dictionary as key value pairs

+2
source

, : http://nirajrules.wordpress.com/2009/01/19/inside-dependencyobject-dependencyproperty/

, , Hashtable . , , HashTable . , Get Set .

:

public class Something
{
  public static readonly DependencyProperty IsEditableProperty = DependencyProperty.Register("IsEditable", typeof(Boolean), typeof(ResourceCanvas), new PropertyMetadata(true));

    public Boolean IsEditable
    {
        get { return (Boolean)this.GetValue(IsEditableProperty); }
        set { this.SetValue(IsEditableProperty, value); }
    }
 }

Something, "" IsEditable.

+2

All Articles