将 ValidationRule 和值转换器组合用于文本框
我有一个简单的问题,我只是找不到好的解决方案。 我有一个绑定到双属性值的文本框。用户可以在文本框中输入值,但我只想允许 0 到 100 之间的值。如果在文本框仍具有焦点时输入无效值,我希望在文本框周围显示一个红色框 (UpdateSourceTrigger="PropertyChanged" )。如果用户单击远离文本框的位置,我想使用 UpdateSourceTrigger="LostFocus" 上的值转换器来限制该值。
执行验证规则或转换器都很容易,但我无法将它们组合起来,因为我希望验证在 UpdateSourceTrigger="PropertyChanged" 上触发,而转换器应在 UpdateSourceTrigger="LostFocus" 上触发。不幸的是,在 TextBox.Text 上设置绑定时,我只能选择其中之一。
关于如何实现此功能有什么好的想法吗?
谢谢你
/彼得
I have a simple problem that I just cant find a good solution for.
I have a textbox bound to a double property value. The user can enter values into the textbox, but I only want to allow values between 0 and 100. I would like to show a red box around the textbox if an invalid value is entered while the textbox still has focus (UpdateSourceTrigger="PropertyChanged"). Should the user click away from the textbox, I want to clamp the value using a value converter on UpdateSourceTrigger="LostFocus".
Its easy to do either the validation rule or the converter, but I can not combine them as I want the validation to trigger on UpdateSourceTrigger="PropertyChanged" and the converter should trigger on UpdateSourceTrigger="LostFocus". Unfortunately I can only choose either one or the other when setting up the binding on my TextBox.Text.
Any good ideas about how I could implement this functionality?
Thank you
/Peter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个有趣的问题。我不确定我是否有完整的解决方案,但我想提出一些想法。
您认为创建一个派生自 TextBox 的新类怎么样?它可以有两个依赖属性:MinValue 和 MaxValue。然后它可以覆盖 OnLostFocus。 (免责声明:我尚未测试以下代码。)
这将消除对转换器的需要,并且您的绑定可以使用 UpdateSourceTrigger=PropertyChanged 来支持您的验证规则。
诚然,我的建议有其缺点。
That's an interesting question. I'm not sure I have a complete solution, but I'd like to throw out a couple ideas.
What do you think of creating a new class that derives from TextBox? It could have two dependency properties, MinValue and MaxValue. Then it could override OnLostFocus. (Disclaimer: I haven't tested the following code.)
This would eliminate the need for a converter, and your binding can use UpdateSourceTrigger=PropertyChanged to support your validation rule.
My suggestion admittedly has its drawbacks.