WPF:沿着可视化树传播验证错误
我经常在我的应用程序中使用和重复使用用户控件。
例如,如果用户应该输入一个整数,我有一个名为“IntegerEditor”的用户控件,它有一个标题标签、一个用户输入文本框和一个单位标签(英寸、秒等)。 .)。
我的用户控件已经有一个名为“IntegerValidationRule”的验证规则,该规则控制用户输入是否为整数。
我有一个与该验证规则关联的样式,它可以更改控件背景并在工具提示中添加错误消息。
有时,我在 UI 中使用这些基本控件,但我想添加其他验证规则,例如“整数必须大于 1023”,并带有特殊错误消息“1024 以下的 UDP 端口已保留,请选择一个”更高的数字”。
您知道我可以在其他地方添加这些验证规则但仍然会触发我的控件上的样式吗?我考虑过将验证错误沿着可视化树传播,但在网上没有发现任何好处。
谢谢您的回答,
A.Jeanrichard
I often use and re-use usercontrols in my apps.
For example, if a user should enter an integer number, I have a usercontrol named "IntegerEditor", which has a label for the title, a textbox for the user input, and a label for the units (inches, seconds, etc...).
My user control already has a validation rule that I called "IntegerValidationRule", which control if the user input is an integer.
I have a style associated with that validation rule that change the control background and add the error message in a tool tip.
Sometimes, I use these kind of basic controls in my UI, but I want to add other validation rules, such as "The integer must be greater than 1023", with special error messages as "UDP Port under 1024 are reserved, please choose a higher number".
Do you know any way that I could add these validations rules somewhere else but that it would still trigger the style on my control? I thought about propagating the validation error down the visual tree, but found nothing great about that on the net.
Thank you for yours answers,
A.Jeanrichard
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯...您可以响应路由的“验证失败”事件,但在我看来,这比拥有负责协调不同 ViewModel 之间的验证的应用程序级 ViewModel 更困难。
Well... you could respond to routed "validation failed" events, but that'd be more difficult IMO than having an application-level ViewModel that's responsible for coordinating validation between different ViewModels.