views:

200

answers:

2

I created a simple class with a DependencyProperty. When setting the value, I observe that ValidateValueCallback is called before CoerceValueCallback.

On wpftutorial and in other books, it is stated that coercion is called before validation.

A: 

3.5sp1 here.

MSDN seems to imply that validation occurs before coercion. This matches what you have discovered.

I did it myself and ValidateValueCallback comes before CoerceValueCallback. I even read source in Reflector and confirmed the same, although it's hard to follow all that code an know for sure all possible paths.

Interesting. I wonder if it was the other way around in an earlier version.

Edit:

Apparently, when you return a different value from the CoerceValueCallback, the ValidateValueCallback is called again for the new value. Validate-Coerce-Validate.

Joel B Fant
+1  A: 

Yes I've observed the same thing. It then seems that validation really does come before coercion. When a new value is set, it is validated. If coercion changes/constraints a value, it has to be validated again. So technically, validation always happens right after a "set," if we view coercion to be just another "set."

I guess the other sites/books are making an assumption, albeit a confusing one, that validation specifically happens after coercion. It seems to happen after any "set."