输入验证 Silverlight
我有一个未绑定的文本框。
<TextBox x:Name="inputBox" Grid.Column="1" Grid.Row="1" />
文本框仅接受数字(双精度),并在将其他内容(字母或符号)写入框中时显示警告。
在 TextChanged 事件中,我根据输入的值进行一些计算并将其显示在 TextBlock 中,因此我需要某种方法来验证输入是用户在框中写入的数字,但我很难找到一个好方法来做到这一点。
有什么想法吗?
i have a textbox which is not bound.
<TextBox x:Name="inputBox" Grid.Column="1" Grid.Row="1" />
The textbox is to only accept numbers (doubles) and show a warning at once something else(letters or symbols) is written in to the box.
On the TextChanged event, i do some calculations depending on the value enterd and show it in a TextBlock, therefor i need some way of validating that the input is a number as the user writes in the box, but i am having a hard time finding a good way to do this.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我之前使用的是正则表达式来禁止非数字字符。也许这是可以适应的东西?
我的代码是针对服务器上的端口的,因此只有数字,但应该很容易添加 .对于双打(我认为“[^0-9\.]”应该有效,但正则表达式不是我非常擅长的东西:-))
What I have used before is a regex to disallow non-numeric characters. Maybe this is something that could be adapted?
My code was for a port on a server so only numbers but should be straightforward to add the . for doubles (I think "[^0-9\.]" should work but regexs aren't something I am fantastically good at :-) )
这是何时使用行为的另一个示例。
您没有真正解释当用户输入无效值时您想要采取什么操作。
This is another example of when to use a Behavior.
You didn't really explain what actions you wanted to take when the user enters an invalid value.
也许最好使用
Binding
和ValueConverter
来更新 TextBlock 的内容。在这种情况下,您可以在转换器内实现数值验证。Maybe it would be better to use
Binding
withValueConverter
that will update TextBlock's content. In this case you can implement validation for numeric value in within converter.