如何让 WPF TextBox 关心小数点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15067827/
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 can I make a WPF TextBox care about a decimal point?
提问by Jordan Carroll
So I have a TextBoxthat only allows numbers and decimal points. I want the user to only be allowed to enter 1 decimal point.
所以我有一个TextBox只允许数字和小数点。我希望用户只能输入 1 个小数点。
Here is the code that is triggered on the PreviewTextInputevent: (Some of the code is a little redundant, but it does the job)
这是在事件上触发的代码PreviewTextInput:(有些代码有点多余,但它可以完成工作)
private void PreviewTextInput(object sender, TextCompositionEventArgs e)
{
TextBox textBox = (TextBox)sender;
if (e.Text == ".")
{
if (textBox.Text.Contains("."))
{
e.Handled = true;
return;
}
else
{
//Here I am attempting to add the decimal point myself
textBox.Text = (textBox.Text + ".");
e.handled = true;
return;
}
}
else
{
e.Handled = !IsTextAllowed(e.Text);
return;
}
}
private static bool IsTextAllowed(string text)
{
Regex regex = new Regex("[^0-9.-]+"); //regex that matches disallowed text
return !regex.IsMatch(text);
}
The problem isthat the first decimal point that is entered is not "significant" until it is followed by a number. So, if the user enters 123.and you were to set a breakpointand check the value of textBox.textit would be 123. I know this is happening because the textBoxis bound to a Doubleso it is trying to be "smart" and forget about those currently "insignificant" values (The ".").
问题是输入的第一个小数点在其后跟一个数字之前并不“重要”。因此,如果用户输入123.并且您要设置 abreakpoint并检查textBox.text它的值将是123。我知道这正在发生,因为textBox它绑定到 aDouble所以它试图变得“聪明”并忘记那些当前“无关紧要”的值(“。”)。
There shouldn't be anything wrong with my code, I was just hoping to force the textBoxto hopefully skip over some unnecessary(?) auto-formatting.
我的代码应该没有任何问题,我只是希望强制textBox跳过一些不必要的(?)自动格式化。
Is there a way to make the textBox"care" about that first decimal point?
有没有办法让textBox“关心”第一个小数点?
Possible Duplicatethat was never answered.
从未回答过的可能重复。
or
或者
*Is there a different approach to limiting the number of decimals?" (I have done a lot of research in this area, and I don't think there are any other options.)
*有没有不同的方法来限制小数位数?”(我在这方面做了很多研究,我认为没有其他选择。)
回答by tam
if just limiting the charcters you want maybe something like binding with the string format would meet your needs
如果只是限制你想要的字符,那么像绑定字符串格式之类的东西就可以满足你的需求
hereis a good example of the format for double
这是 double 格式的一个很好的例子
And this would be an example of doing the binding in code to your ViewModel
这将是在代码中绑定到您的 ViewModel 的示例
<TextBox Text="{Binding LimitedDouble,StringFormat={}{0:00.00}}"></TextBox>
回答by Sandeep.R.Nair
private void txtDecimal_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '\b' && e.KeyChar!='.')
{
e.Handled = true;
}
if (e.KeyChar == '.' && txtDecimal.Text.Contains("."))
{
e.Handled = true;
}
}

