#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

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 131 other followers