CoreceValueCallback和ValidateValueCallback之间的区别? [英] Difference between CoreceValueCallback and ValidateValueCallback?

查看:158
本文介绍了CoreceValueCallback和ValidateValueCallback之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 CoerceValueCallback 用于更正值,并且 ValidateValueCallback 将返回true或false。但是我的问题是为什么我们需要 ValidatevalueCallback ?我们可以简单地使用 CoerceValueCallback 进行验证(使用if条件)并更正该值。您能否举一些实际的例子说明何时使用强制与验证?

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?

推荐答案

值强制基本上是用来更改值,如果新值不符合系统预期。最好的示例是Slider控件。滑块具有最小和最大属性。显然,如果允许最大值降至最小值以下将是一个问题。值强制用于防止发生这种无效状态。

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.

验证值是系统只会检查给定输入是否有效的值。如果value无效(如果我们为该值返回false),它将抛出Argument Exception。例如,我们具有Age属性,并且它的范围应该在0到120之间。如果新值是500,系统可能会警告用户,而不是将其强制为某个硬编码值。

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.

这篇关于CoreceValueCallback和ValidateValueCallback之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆