TextBox应该只允许Silverlight中的数字和一个DOT(。) [英] TextBox should only allow numbers and one DOT (.) in Silverlight

查看:62
本文介绍了TextBox应该只允许Silverlight中的数字和一个DOT(。)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>帮我任何一个,



我的要求是文本框必须只允许1到9和一个DOT(。),这应该由REGULAR EXPRESSION完成。 < b> IN文本框的任何位置都不应该允许使用字母< / b>

如44rt.56rt / R45.9t9



如果以上方式不可能,请建议任何替代方法来实现此目的。



注意:我使用Silverlight使用MVVM模式,我没有太多想写任何文本代码背后的盒子事件。



问候



斯里兰卡

>Help me any one ,

My requirment is textbox must allow only 1 to 9 and one DOT (.) , this should be done by REGULAR EXPRESSION. <b>IN Any position of the text box should not allow alphabets</b>
like 44rt.56rt / R45.9t9

If the above way is not possible kindly advice any alternate way to accomplish this.

Note: Iam using MVVM patern with Silverlight , iam not much intested to write any Text Box events at code behind.

Regards

Srilatha

推荐答案

它与正则表达式无关。您需要处理事件 System.Windows.UIElement.TextInput 。此事件可以取消,这是通过赋值为true来完成的属性 System.Windows.RoutedEventArgs.Handled 运行时类型将有所不同,请参阅下面)传递给你的事件处理程序。您需要检查属性 System.Windows.TextCompositionEventArgs.Text 的值,也在同一个事件参数对象中传递并查看它是否适合输入。如果不是,则将true指定给 Handled ,以忽略更改。这个文本应该是用户试图输入的一个字符。



请参阅:

http://msdn.microsoft.com/en-us/library/system。 windows.uielement.textinput%28v = vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventhandler.aspx [<一个href =http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventhandler.aspx\"target =_ blanktitle =New Window> ^ ],

http://msdn.microsoft.com/en-我们/库/ system.windows.input.t extcompositioneventargs.aspx [ ^ ],

http: //msdn.microsoft.com/en-us/library/system.windows.routedeventargs.handled.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventargs.text.aspx [ ^ ]。



类似于:

It has nothing to do with Regular Expression. You need to handle the event System.Windows.UIElement.TextInput. This event can be canceled, which is done by assigned true to the property System.Windows.RoutedEventArgs.Handled (runtime type will be different, see below) passed to your event handler. You will need to check up the value of the property System.Windows.TextCompositionEventArgs.Text, also passed in the same event arguments object and see if it is good for input or not. If not, assign true to Handled, to ignore the change. This text is supposed to be just one character the user tries to type.

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.textinput%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventhandler.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventargs.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.routedeventargs.handled.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventargs.text.aspx[^].

Something like:
myTextBox.TextInput += (sender, eventArgs) => {
   bool isGoodInput = CheckIfYouWantThisInput(eventArgs.Text); // don't forget to allow #8 (backspace) which is also considered as text, by some historical reasons
   eventArgs.Handled = !isGoodInput; // ignore then
};





-SA


这篇关于TextBox应该只允许Silverlight中的数字和一个DOT(。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆