windows phone中textbox只能输入数字 .

两种方法  

第一种比较简单,直接设置textbox的属性,属性是InputScope

第二种就比较麻烦,需要添加一个类,使该类继承于textbox,然后设置其只能输入数字,需要在xaml页面添加命名空间,然后引用就行了

设置类的代码:

View Code
 1 private readonly Key[] numeric = new Key[] { Key.Back, Key.NumPad0, Key.NumPad1, Key.NumPad2, Key.NumPad3, Key.NumPad4, Key.NumPad5, Key.NumPad6, Key.NumPad7, Key.NumPad8, Key.NumPad9 }; 
 2           public NumericTextBox()      
 3           {            //将文本设置为  电话号码的文本输入模式   
 4               this.Width = 300.0;
 5               this.Height = 80.0;
 6               this.InputScope = new InputScope();        
 7               this.InputScope.Names.Add(new InputScopeName() { NameValue = InputScopeNameValue.TelephoneNumber });  
 8           }   
 9          protected override void OnKeyDown(KeyEventArgs e)   
10          {  //如果是数字键或者返回键则设置e.Handled = true; 表示事件已经处理    
11              if(Array.IndexOf(numeric,e.Key) == -1)           
12              {               
13                  e.Handled = true;         
14              }           
15              base.OnKeyDown(e); // important, if not called the back button is not handled     
16          }

 

posted @ 2012-11-24 18:42  qinpei  阅读(354)  评论(1编辑  收藏  举报