积极答复者
WPF怎么验证查询条件中的文本框只允许输入数字啊?

问题
答案
-
楼主你好:
基本思路就是监听keyboard的轮入事件(如KeyDown,PreviewTextInput),然后再写一些验证逻辑,下面是网上一个例子:
public class NumberTextBox : TextBox
{
protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
{
e.Handled = !AreAllValidNumericChars(e.Text);
base.OnPreviewTextInput(e);
}
bool AreAllValidNumericChars(string str)
{
bool ret = true;
if (str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator |
str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyGroupSeparator |
str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencySymbol |
str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeSign |
str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeInfinitySymbol |
str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator |
str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberGroupSeparator |
str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentDecimalSeparator |
str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentGroupSeparator |
str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentSymbol |
str == System.Globalization.NumberFormatInfo.CurrentInfo.PerMilleSymbol |
str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveInfinitySymbol |
str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveSign)
return ret;
int l = str.Length;
for (int i = 0; i < l; i++)
{
char ch = str[i];
ret &= Char.IsDigit(ch);
}
return ret;
}
}
谢谢。
Jim Zhou -MSFT- 已标记为答案 Jim Zhou - MSFTModerator 2009年11月24日 7:54
-
如果不愿意写这么多代码 可以用正则表
protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e){
e.Handled = !System.Text.RegularExpressions.Regex.IsMatch (e.Text,@"\d+");
base.OnPreviewTextInput(e);
}
- 已标记为答案 Jim Zhou - MSFTModerator 2009年11月25日 8:11
-
楼主你好,
-->WPF里面有个validaterule,那个在不绑定数据字段的情况下能使用吗?
据我所知,ValidationRule只有在你对TextBox进行数据绑定的时候起作用。
-->楼上的方法可以部分解决问题,但对框里没有在框里按键,框里默认的数据就是错误这样的情况还是不方便。
上面的的例子只是一个demo,并不能包含任何具体的情况。要是你也要验证开始就在TextBox中的值,当然可以在其constructor中调用OnPreviewTextInput中的代码进行验证。
谢谢。
Jim Zhou -MSFT- 已标记为答案 滚地龙 2009年11月25日 8:15
全部回复
-
楼主你好:
基本思路就是监听keyboard的轮入事件(如KeyDown,PreviewTextInput),然后再写一些验证逻辑,下面是网上一个例子:
public class NumberTextBox : TextBox
{
protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
{
e.Handled = !AreAllValidNumericChars(e.Text);
base.OnPreviewTextInput(e);
}
bool AreAllValidNumericChars(string str)
{
bool ret = true;
if (str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator |
str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyGroupSeparator |
str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencySymbol |
str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeSign |
str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeInfinitySymbol |
str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator |
str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberGroupSeparator |
str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentDecimalSeparator |
str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentGroupSeparator |
str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentSymbol |
str == System.Globalization.NumberFormatInfo.CurrentInfo.PerMilleSymbol |
str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveInfinitySymbol |
str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveSign)
return ret;
int l = str.Length;
for (int i = 0; i < l; i++)
{
char ch = str[i];
ret &= Char.IsDigit(ch);
}
return ret;
}
}
谢谢。
Jim Zhou -MSFT- 已标记为答案 Jim Zhou - MSFTModerator 2009年11月24日 7:54
-
如果不愿意写这么多代码 可以用正则表
protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e){
e.Handled = !System.Text.RegularExpressions.Regex.IsMatch (e.Text,@"\d+");
base.OnPreviewTextInput(e);
}
- 已标记为答案 Jim Zhou - MSFTModerator 2009年11月25日 8:11
-
楼主你好,
-->WPF里面有个validaterule,那个在不绑定数据字段的情况下能使用吗?
据我所知,ValidationRule只有在你对TextBox进行数据绑定的时候起作用。
-->楼上的方法可以部分解决问题,但对框里没有在框里按键,框里默认的数据就是错误这样的情况还是不方便。
上面的的例子只是一个demo,并不能包含任何具体的情况。要是你也要验证开始就在TextBox中的值,当然可以在其constructor中调用OnPreviewTextInput中的代码进行验证。
谢谢。
Jim Zhou -MSFT- 已标记为答案 滚地龙 2009年11月25日 8:15