wpfdependency-propertiesdependencyobject

How WPF DependencyProperty pickup the key to distinguish each instance of an object and seek the values of the DependencyProperty?


From google, a lot of articles said that DependencyProperty is static because it has a KeyValue mechanism to maintain the value of each instance of the object.

But the problem is that, if we call GetValue / SetValue against the DependencyProperty, how does it identify each instance and generate the key so as to read / save the values from the HashTable for different instance of the object?

For example: if we create 2 instance of TestDp and then set value for TestDProperty of both instances, how does SetValue identify each instance and save the DependencyProperty value into the hash table accordingly?

I've checked the code for GetValue & SetValue of DependencyObject, but I still cannot figure out how it distinguish each instance. The code this.LookupEntry(dp.GlobalIndex) will pickup the EntryIndex but I'm not sure how the GlobalIndex is generated to distinguish each instance of the object.

 public class TestDp : DependencyObject
    {
        protected static DependencyProperty dpTest = DependencyProperty.Register("TestDProperty", typeof(string), typeof(TestDp));
        public string TestDProperty
        {
            get
            {
                var r = (string)GetValue(dpTest);
                return r;
            }
            set
            {
                SetValue(dpTest, value);
            }
        }
    }

Solution

  • The GetValue of DependencyObject will call GetValueEntry and consume the EntryIndex which you've mentioned. However, it doesn't seems to generate any key based on the instance information.