#151 – Dependency Properties Remember Non-Coerced Values
December 10, 2010 1 Comment
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).
Pingback: #152 – Use ReadLocalValue() to Find the Local Value of a Dependency Property « 2,000 Things You Should Know About WPF