您的位置:首页 > 其它

WPF中TextBox限制输入方法

2010-03-22 11:23 267 查看
用PreviewKeyDown事件和KeyEventArgs.Handled 来实现

示例:限制了只能输入1和退格键

XAML

<TextBox HorizontalAlignment="Left" Margin="0,60,0,0" Name="textBox1" Width="120" Height="22" VerticalAlignment="Top" PreviewKeyDown="textBox1_PreviewKeyDown" />

C#

private void textBox1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.D1 && e.Key != Key.Back && e.Key !=Key.NumPad1)
{
e.Handled = true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: