设置 DependencyProperty 值时,验证是在强制转换之前还是之后调用?
我创建了一个带有 DependencyProperty 的简单类。 设置值时,我观察到 ValidateValueCallback 在 CoerceValueCallback 之前被调用。
在 wpftutorial 和其他书籍中,有说明在验证之前调用强制转换。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里是3.5sp1。
MSDN 似乎暗示验证发生在强制之前。 这与您所发现的相符。
我自己做的,
ValidateValueCallback
出现在CoerceValueCallback
之前。 我什至阅读了 Reflector 中的源代码并确认了相同的内容,尽管很难遵循所有代码并知道确保所有可能的路径。有趣的。 我想知道早期版本中是否是相反的。
编辑:
显然,当您从
CoerceValueCallback
返回不同的值时,会再次调用ValidateValueCallback
以获取新值。 验证-强制-验证。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 beforeCoerceValueCallback
. 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
, theValidateValueCallback
is called again for the new value. Validate-Coerce-Validate.是的,我也观察到同样的事情。 看来验证确实先于强制。 当设置新值时,它会被验证。 如果强制改变/约束一个值,则必须再次验证它。 因此,从技术上讲,如果我们将强制转换视为另一个“集合”,那么验证总是在“集合”之后立即发生。
我猜其他网站/书籍正在做出一个假设,尽管这是一个令人困惑的假设,即验证是在强制之后发生的。 这似乎发生在任何“设定”之后。
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."