点滴积累,融会贯通

-----喜欢一切有兴趣的东西

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

用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;
            }
        }

 

 

posted on 2010-03-22 11:23  小寒  阅读(4535)  评论(0编辑  收藏  举报