Difference between CoreceValueCallback and Validat

2020-02-26 11:28发布

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?

2条回答
贪生不怕死
2楼-- · 2020-02-26 12:18

Value coercion is basically to change the value, if the the new value is not as system expected. A best example is Slider control. A Slider has both Minimum and Maximum properties. Clearly, it would be a problem if the Maximum value were allowed to fall below the Minimum value. Value coercion is used to prevent this invalid state from occuring.

Validate value, is something that system will only check whether the given input is valid or not. It will throw Argument Exception if value is invalid (if we returned false for such value). For example, we have Age property, and it should be in range of 0 to 120. In case the new value is 500, the system may warn the user instead coercing it to some hardcoded value.

Any way both callbacks are optional and can be used based on the requirement.

查看更多
贼婆χ
3楼-- · 2020-02-26 12:21

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.

查看更多
登录 后发表回答