Windows Phone 7 - 擴展TextBox控件為數字輸入文本框
有一些信息的錄入,比如電話號碼,郵編等等的這些信息都只是需要輸入數字,而Windows Phone 7里面的控件并沒有只讓輸入數字的一個控件,那么要實現這樣的一個控件就只能夠手工地去擴展TextBox控件了。
擴展一個控件的步驟:
1、定義一個類,這個類需要繼承你要擴展的控件的類
public class NumericTextBox : TextBox2、在頁面上添加擴展的控件的類的空間引用
xmlns:my="clr-namespace:WPNumericTextBox.Controls"3、調用控件
NumericTextBox.cs
using System;
using System.Windows.Controls;
using System.Windows.Input;
namespace WPNumericTextBox.Controls
{
public class NumericTextBox : TextBox
{
//返回鍵和數字鍵
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 };
public NumericTextBox()
{
//將文本設置為 電話號碼的文本輸入模式
this.InputScope = new InputScope();
this.InputScope.Names.Add(new InputScopeName() {
NameValue = InputScopeNameValue.TelephoneNumber });
}
protected override void OnKeyDown(KeyEventArgs e)
{ //如果是數字鍵或者返回鍵則設置e.Handled = true; 表示事件已經處理
if(Array.IndexOf(numeric,e.Key) == -1)
{
e.Handled = true;
}
base.OnKeyDown(e); // important, if not called the back button is not handled
}
}
}
本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!