2013-07-06 16 views
6

WPFで数字だけを受け入れるテキストボックスを作成したい... 私はreaserchedして、人々はkeypressイベントやマスクされたテキストボックスを使用すると言いますが、それらはWindowsフォームにあります...C#の数値TextBox - WPF

WPFのために

答えて

32

:Windowsフォームの場合

private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e) 
{ 
    if (!char.IsDigit(e.Text, e.Text.Length - 1)) 
     e.Handled = true; 
} 

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (!char.IsDigit(e.KeyChar)) 
     e.Handled = true; 
} 
+5

は – javirs

+0

簡単に感謝MaciekとJ3soonを解決するようなコードによって制御されることはありません、あなたのボックスにコピー&ペーストに注意してください –