WPF KeyUp for enter key in main window after messagebox 使用keyup事件获取键盘"回车"操作,判断并弹出消息框,回车关闭消息框,它再次弹出

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/4c404667-e766-4e5c-9116-a78febbc29d8

 

This is correct behaviour. Buttons respond to the enter key being pressed, not released, so the message box is dismissed as soon as the key is pressed and its parent form activated again. So, the parent form is activated by the time the key is release and it gets the key up message.

The incorrect behaviour in this case is your application showing a message box on a key up. It should either show it on key down, or wait for a full key down then key up cycle to display it.

 

解决方法,使用keydown方法替代,或者使用一组完整的keydown和keyup事件处理

 

private void textBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e) { if (e.Key == System.Windows.Input.Key.Enter) { // MessageBox.Show("OK"); } }

你可能感兴趣的:(WPF KeyUp for enter key in main window after messagebox 使用keyup事件获取键盘"回车"操作,判断并弹出消息框,回车关闭消息框,它再次弹出)