#151 – Dependency Properties Remember Non-Coerced Values

When you set a dependency property to some value and it is coerced, the original base value that you set is remembered.  If you remove the conditions that led to the coercion, the property will take on the non-coerced value that you originally set.

Suppose we have a Person class with an Age property and a boolean SuperOld property.  Assume that Age is coerced to a value of 999 if SuperOld is true.

If you set Age to some local value and SuperOld is true, Age will get coerced to 999.  But if you later set SuperOld to false, the Age property will revert to the last local value that you tried to set.

            Person p = new Person("Methuselah");

            p.Age = 28;
            p.SuperOld = true;    // Age coerced to 999

            p.Age = 56;           // Age still 999
            p.SuperOld = false;   // Age now becomes 56

This assumes that the PropertyChanged callback for SuperOld calls CoerceValue on the Age property.  (Likely true).


About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #151 – Dependency Properties Remember Non-Coerced Values

  1. Pingback: #152 – Use ReadLocalValue() to Find the Local Value of a Dependency Property « 2,000 Things You Should Know About WPF

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: