wpf 如何在文本框中只接受数字和一个句点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12208750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to accept only numbers and a single period on textbox
提问by l46kok
I have a WPF textbox that is databound. I need to restrict the user input on the textbox so that it only accepts numbers and a single period (For displaying decimals).
我有一个数据绑定的 WPF 文本框。我需要限制文本框上的用户输入,以便它只接受数字和一个句点(用于显示小数)。
I know I can handle this in "Winforms" way and validate each input on KeyPress event, but I was wondering if there was a cleaner and maybe even proper way to do this in WPF (especially since I am databinding the textbox).
我知道我可以用“Winforms”的方式处理这个问题并验证 KeyPress 事件上的每个输入,但我想知道是否有更干净的方法,甚至可能是在 WPF 中执行此操作的正确方法(尤其是因为我正在对文本框进行数据绑定)。
回答by Kash
Use ValidationRules provided by WPF.
使用 WPF 提供的 ValidationRules。
The xaml would be:
xaml 将是:
<TextBox>
<TextBox.Text>
<Binding Path="Name">
<Binding.ValidationRules>
<ExceptionValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
The code for the textbox property would be (used regex for the validation):
文本框属性的代码将是(使用正则表达式进行验证):
public string Name
{
get { return _name; }
set
{
_name = value;
if (!Regex.IsMatch(value, @"^((?:[1-9]\d*)|(?:(?=[\d.]+)(?:[1-9]\d*|0)\.\d+))$"))
{
throw new ApplicationException("Please enter only numbers/decimals.");
}
}
}
Source: Validation in WPF
来源:WPF 中的验证
The regex given above: ^((?:[1-9]\d*)|(?:(?=[\d.]+)(?:[1-9]\d*|0)\.\d+))$can be tested at this Rubular link
上面给出的正则表达式:^((?:[1-9]\d*)|(?:(?=[\d.]+)(?:[1-9]\d*|0)\.\d+))$可以在这个Rubular 链接上测试
The regex would match these:
正则表达式将匹配这些:
1.2
22522
0.33
3.90000
but not these: (you could tweak the regex to allow some of them)
但不是这些:(您可以调整正则表达式以允许其中一些)
.999
23.35.1343
03423.23423
回答by Justin
Databinding will affect the values passed to/from the object you're databound to. To stop the user from pressing keys you either need to use a masked text box (in winforms, not sure about WPF) or you need to handle the KeyPressedEvent in the textbox and stop the keys you don't want pressed from happening.
数据绑定将影响传入/传出数据绑定到的对象的值。要阻止用户按键,您需要使用屏蔽文本框(在 winforms 中,不确定 WPF),或者您需要处理文本框中的 KeyPressedEvent 并阻止您不想按下的键发生。
I have used the code below to only allow digits and one decimal
我使用下面的代码只允许数字和一位小数
private void textBoxPrice_KeyPress( object sender, KeyPressEventArgs e )
{
if( !char.IsControl( e.KeyChar )
&& !char.IsDigit( e.KeyChar )
&& e.KeyChar != '.' )
{
e.Handled = true;
}
// only allow one decimal point
if( e.KeyChar == '.'
&& ( sender as TextBox ).Text.IndexOf( '.' ) > -1 )
{
e.Handled = true;
}
}
回答by VIRA
just use keypress event, and validate the key press event with ascii character.
只需使用按键事件,并使用 ascii 字符验证按键事件。
e.KeyCode >47 && e.KeyCode <58 would be restricting user not to press any letters apart from numbers.
e.KeyCode >47 && e.KeyCode <58 会限制用户不要按数字以外的任何字母。
If you need exact code sample, wait for a while :)
如果您需要确切的代码示例,请稍等 :)

