Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between CoreceValueCallback and ValidateValueCallback?

I know that CoerceValueCallback is used to correct a value and that ValidateValueCallback will return true or false. But my question is why we need ValidatevalueCallback? We can simply use CoerceValueCallback to validate (using if condition) and correct the value. Can you give some practical example of when to use coercion vs. validation?

like image 588
Ravuthasamy Avatar asked Jun 26 '13 03:06

Ravuthasamy


1 Answers

Here are the rules I follow for when to use coercion vs. validation.

Use CoerceValueCallback If...

  • You can safely correct a value to be valid without needing to throw an error.
  • Your property depends on one or more other dependency properties.
  • You need to provide instance-level validation as opposed to class-level validation.
  • You allow others to override your validation logic.

Use ValidateValueCallback If...

  • You cannot correct a value to be valid.
  • You must throw an error if an invalid value is provided.
  • You do not want others to override your validation logic.

So, it primarily depends on whether or not your property depends on other dependency properties or if you want others to be able to override your validation logic.

Since the ValidateValueCallback is not part of the PropertyMetadata, inheritors cannot modify the callback through the DependencyProperty.OverrideMetadata function.

Also, since the ValidateValueCallback does not provide your DependencyObject as a parameter, you cannot perform advanced validation that depends on other dependency properties.

Example 1

Suppose you have Minimum, Maximum, & Value properties. When any of these change, a CoerceValueCallback shoud be used to ensure the other properties are consistent.
That is, Minmum <= Value <= Maximum.

However, assuming these values are doubles, then there are some values that would never make sense, namely Double.NaN, Double.PositiveInfinity, and Double.NegativeInfinity. Therefore, a ValidateValueCallback should be used to verify that the double values are normal, numeric values.

In fact, this is exactly how RangeBase works!

Example 2

Suppose you have a RegexTextBox control which takes a string containing a regular expression (call it RegexString). If a bad regular expression is provided, then what should be used instead? It might make sense to coerce it to be a null/empty value, rendering it useless; however, I suggest that this property be validated with a ValidateValueCallback. This is because any error is now thrown at compile-time when designing via the WPF designer.

For this property, there shouldn't be a CoerceValueCallback at all.


There is a whole lot of information describing how to use these callbacks. I'd suggest taking a look at the MSDN article, Dependency Property Callbacks and Validation, for more in-depth knowledge.
like image 172
Nicholas Miller Avatar answered Oct 13 '22 00:10

Nicholas Miller