设置 DependencyProperty 值时,验证是在强制转换之前还是之后调用?

发布于 2024-07-06 16:22:25 字数 265 浏览 6 评论 0原文

我创建了一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彩扇题诗 2024-07-13 16:22:25

这里是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 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.

忘年祭陌 2024-07-13 16:22:25

是的,我也观察到同样的事情。 看来验证确实先于强制。 当设置新值时,它会被验证。 如果强制改变/约束一个值,则必须再次验证它。 因此,从技术上讲,如果我们将强制转换视为另一个“集合”,那么验证总是在“集合”之后立即发生。

我猜其他网站/书籍正在做出一个假设,尽管这是一个令人困惑的假设,即验证是在强制之后发生的。 这似乎发生在任何“设定”之后。

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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文