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);
}
}
}
GetValue
/SetValue
on current instance ofDependencyObject
. AlsodpTest
should be namedTestDProperty
and CLR wrapper should be calledTestD
and name of registered property "TestD" - dkozlSetValue
on different instances, so it knows where to save the data separately. Note that the data are shared only when they have the default value, once they change for some specific instance, they will no longer be shared. - King KingDependencyProperty
definition is static butthis.GetValue(...)
andthis.SetValue(...)
are instance methods ofDependencyObject
so will be called on current instance just asthis.LookupEntry(...)
- dkozl