Windows Phone 7文本框验证。我只想要数字

时间:2010-12-30 19:24:50

标签: validation windows-phone-7 textbox

我似乎无法让这个工作起来..在WP7上,就e.Key而言,'%'与'5'相同。我如何才能获得数字......

或者是否有一个开放式的WP7文本框控件可以像AJAX控件工具包一样工作,所以我可以说..这是一个TextBox ..只允许数字..?

if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
        {
            e.Handled = false;
            return;
        }

然后整个功能

  private void HandleKeyEvent(KeyEventArgs e)
    {
        e.Handled = true;

        if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
        {
            e.Handled = false;
            return;
        }

        if (e.Key == Key.Back || e.Key == Key.Delete || e.Key == Key.Left || e.Key == Key.Right ||
            e.Key == Key.D0 || e.Key == Key.D1 || e.Key == Key.D2 || e.Key == Key.D3 || e.Key == Key.D4 || e.Key == Key.D5 || e.Key == Key.D6 ||
            e.Key == Key.D7 || e.Key == Key.D8 || e.Key == Key.D9 ||
            e.Key == Key.NumPad0 || e.Key == Key.NumPad1 || e.Key == Key.NumPad2 || e.Key == Key.NumPad3 || e.Key == Key.NumPad4 || e.Key == Key.NumPad5 || e.Key == Key.NumPad6 ||
            e.Key == Key.NumPad7 || e.Key == Key.NumPad8 || e.Key == Key.NumPad9)
        {
            e.Handled = false;
            //return;
        }

        if ((e.Key == Key.Subtract || (e.Key == Key.Unknown && e.PlatformKeyCode == 189)) && base.SelectionStart == 0 && (base.Text.Length == 0 || base.Text[0] != '-'))
        {
            e.Handled = false;
        }

        if (this.Text.Length > 3)
        {
            e.Handled = true;
        }    
    }

1 个答案:

答案 0 :(得分:6)

以下链接对您很有用我相信TextBox Numbers Only WP7。它详细解释了你的需求。