#152 – Use ReadLocalValue() to Find the Local Value of a Dependency Property

You can use a DependencyObject‘s ReadLocalValue method to get the local value of a dependency property, rather than its effective value.  Recall that the local value is a value set locally, from XAML or code, which takes precedence over all other sources for the base value of a dependency property.  The local value has lower precedence than any coerced values.

Using ReadLocalValue (assuming that this refers to a DependencyObject that registers AgeProperty):

            int ageValue = (int)this.GetValue(AgeProperty);         // Effective value
            int ageLocal = (int)this.ReadLocalValue(AgeProperty);   // Local value

Getting this information for the Age/SuperOld example, we get the following:

            p.Age = 28;
            info = p.AgeInfo();   // Value=28, Local=28

            p.SuperOld = true;
            info = p.AgeInfo();   // Value=999, Local=28

            p.Age = 56;
            info = p.AgeInfo();   // Value=999, Local=56

            p.SuperOld = false;
            info = p.AgeInfo();   // Value=56, Local=56