Windows phone 7 如何在WP7中添加可以接受浮点值的数字文本框?

Windows phone 7 如何在WP7中添加可以接受浮点值的数字文本框?,windows-phone-7,silverlight-4.0,textbox,floating-point,inputscope,Windows Phone 7,Silverlight 4.0,Textbox,Floating Point,Inputscope,我正在开发WP7应用程序。我是WP7的新手。我对silverlight也是新手。我的应用程序中有一个文本框。在此文本框中,用户输入金额。我想在我的应用程序中提供贷款,以便用户可以输入浮动金额(例如1000.50或499.9999)。用户应该能够在“.”之后输入两位数或四位数。我的文本框代码如下 <TextBox InputScope="Number" Height="68" HorizontalAlignment="Left" Margin="-12,0,0,141" Name="Amou

我正在开发WP7应用程序。我是WP7的新手。我对silverlight也是新手。我的应用程序中有一个文本框。在此文本框中,用户输入金额。我想在我的应用程序中提供贷款,以便用户可以输入浮动金额(例如1000.50或499.9999)。用户应该能够在“.”之后输入两位数或四位数。我的文本框代码如下

<TextBox InputScope="Number" Height="68" HorizontalAlignment="Left" Margin="-12,0,0,141" Name="AmountTextBox" Text="" VerticalAlignment="Bottom" Width="187" LostFocus="AmountTextBox_LostFocus" BorderBrush="Gray" MaxLength="10"/>

如何解决上述问题。你能提供我任何代码或链接,通过它我可以解决上述问题。如果我做错了什么,请指导我

下载查尔斯·佩佐尔德的免费书。在第380页的“文本框绑定更新”一节中,在第12章:数据绑定下,他有一个验证浮点输入的优秀示例

要将用户输入限制为2位或4位小数,您必须向
TextBox
TextChanged
回调添加一些逻辑。例如,您可以将浮点转换为字符串,搜索小数点,然后计算小数点右侧字符串的长度


另一方面,如果您只想将用户输入四舍五入为2或4位,请查看第页的定点(“F”)格式说明符部分。

您可以始终使用此正则表达式[-+]?[0-9]。?[0-9]确定其是否为浮点数

public void AmountTextBox_LostFocus(object sender, RoutedEventArgs e)
  {
      Regex myRange = new Regex(@"[-+]?[0-9]*\.?[0-9]");
      if (myRange.IsMatch(textBox1.Text))
        // Match do whatever
      else
        // No match do whatever
  }

下面的代码对我来说很好。我已经在我的应用程序中进行了测试。在下面的代码中,通过添加一些验证,我们可以将常规文本框视为可以接受浮点值的数字文本框

public void AmountTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            foreach (char c in AmountTextBox.Text)
            {                
                if (!char.IsDigit(c) && !(c == '.'))
                {
                    if (c == '-')
                    {
                        MessageBox.Show("Only positive values are allowed");
                        AmountTextBox.Focus();
                        return;
                    }

                    MessageBox.Show("Only numeric values are allowed");
                    AmountTextBox.Focus();
                    return;
                }
            }

            string [] AmountArr = AmountTextBox.Text.Split('.');
            if (AmountArr.Count() > 2)
            {
                MessageBox.Show("Only one decimal point are allowed");
                AmountTextBox.Focus();
                return;
            }

            if (AmountArr.Count() > 1)
            {
                int Digits = AmountArr[1].Count();
                if (Digits > 2)
                {
                    MessageBox.Show("Only two digits are allowed after decimal point");
                    AmountTextBox.Focus();
                    return;
                }
            }
        }

我这样做验证,如下面的代码所示

无需逐字符检查,用户文化得到尊重

namespace Your_App_Namespace
{
public static class Globals
{
    public static double safeval = 0; // variable to save former value!

    public static bool isPositiveNumeric(string strval, System.Globalization.NumberStyles NumberStyle)
    // checking if string strval contains positive number in USER CULTURE NUMBER FORMAT!
    {
        double result;
        boolean test;
        if (strval.Contains("-")) test = false;
        else test = Double.TryParse(strval, NumberStyle, System.Globalization.CultureInfo.CurrentCulture, out result);
        // if (test == false) MessageBox.Show("Not positive number!");
        return test;
    }

    public static string numstr2string(string strval, string nofdec)
    // conversion from numeric string into string in USER CULTURE NUMBER FORMAT!
    // call example numstr2string("12.3456", "0.00") returns "12.34"
    {
        string retstr = "";
        if (Globals.isPositiveNumeric(strval, System.Globalization.NumberStyles.Number)) retstr = double.Parse(strval).ToString(nofdec);
        else retstr = Globals.safeval.ToString(nofdec);
        return retstr;
    }

    public static string number2string(double numval, string nofdec)
    // conversion from numeric value into string in USER CULTURE NUMBER FORMAT!
    // call example number2string(12.3456, "0.00") returns "12.34"
    {
        string retstr = "";
        if (Globals.isPositiveNumeric(numval.ToString(), System.Globalization.NumberStyles.Number)) retstr = numval.ToString(nofdec);
        else retstr = Globals.safeval.ToString(nofdec);
        return retstr;
    }
}

// Other Your_App_Namespace content

}

// This the way how to use those functions in any of your app pages

    // function to call when TextBox GotFocus

    private void textbox_clear(object sender, System.Windows.RoutedEventArgs e)
    {
        TextBox txtbox = e.OriginalSource as TextBox;
        // save original value
        Globals.safeval = double.Parse(txtbox.Text);
        txtbox.Text = "";
    }

    // function to call when TextBox LostFocus

    private void textbox_change(object sender, System.Windows.RoutedEventArgs e)
    {
        TextBox txtbox = e.OriginalSource as TextBox;
        // text from textbox into sting with checking and string format
        txtbox.Text = Globals.numstr2string(txtbox.Text, "0.00");
    }

如果用户输入负数怎么办?第一个
foreach
循环将把它标记为错误。即使只允许正值,在这种情况下显示的错误消息也没有意义。谢谢您的建议。现在我更新了答案。现在用户只能输入正值。如果用户输入负值,将显示相应的消息。但我仍然可以输入表达式,如
2-1
,此代码将其标记为负数。我建议您使用
Double.TryParse()
(或
Single.TryParse()
)首先确定条目是否为有效浮点数,然后仅在条目有效时才执行负数和小数点后位数的检查。
namespace Your_App_Namespace
{
public static class Globals
{
    public static double safeval = 0; // variable to save former value!

    public static bool isPositiveNumeric(string strval, System.Globalization.NumberStyles NumberStyle)
    // checking if string strval contains positive number in USER CULTURE NUMBER FORMAT!
    {
        double result;
        boolean test;
        if (strval.Contains("-")) test = false;
        else test = Double.TryParse(strval, NumberStyle, System.Globalization.CultureInfo.CurrentCulture, out result);
        // if (test == false) MessageBox.Show("Not positive number!");
        return test;
    }

    public static string numstr2string(string strval, string nofdec)
    // conversion from numeric string into string in USER CULTURE NUMBER FORMAT!
    // call example numstr2string("12.3456", "0.00") returns "12.34"
    {
        string retstr = "";
        if (Globals.isPositiveNumeric(strval, System.Globalization.NumberStyles.Number)) retstr = double.Parse(strval).ToString(nofdec);
        else retstr = Globals.safeval.ToString(nofdec);
        return retstr;
    }

    public static string number2string(double numval, string nofdec)
    // conversion from numeric value into string in USER CULTURE NUMBER FORMAT!
    // call example number2string(12.3456, "0.00") returns "12.34"
    {
        string retstr = "";
        if (Globals.isPositiveNumeric(numval.ToString(), System.Globalization.NumberStyles.Number)) retstr = numval.ToString(nofdec);
        else retstr = Globals.safeval.ToString(nofdec);
        return retstr;
    }
}

// Other Your_App_Namespace content

}

// This the way how to use those functions in any of your app pages

    // function to call when TextBox GotFocus

    private void textbox_clear(object sender, System.Windows.RoutedEventArgs e)
    {
        TextBox txtbox = e.OriginalSource as TextBox;
        // save original value
        Globals.safeval = double.Parse(txtbox.Text);
        txtbox.Text = "";
    }

    // function to call when TextBox LostFocus

    private void textbox_change(object sender, System.Windows.RoutedEventArgs e)
    {
        TextBox txtbox = e.OriginalSource as TextBox;
        // text from textbox into sting with checking and string format
        txtbox.Text = Globals.numstr2string(txtbox.Text, "0.00");
    }