【原】WPF按钮或page获取焦点后,按backspace键,后退的解决方法
1.在MainWindow构造函数中添加如下代码,定义路由监听事件
//利用路由事件监听所有的按钮按键事件 frameRoot.AddHandler(Button.PreviewKeyDownEvent, new RoutedEventHandler(Button_PreviewKeyDown));
2.定义Button_PreviewKeyDown
/// <summary> /// 处理backspace导致后退的问题 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_PreviewKeyDown(object sender, RoutedEventArgs e) { if (e.OriginalSource.GetType().Name == typeof(Button).Name || e.OriginalSource.GetType().Name == typeof(Page).Name) { e.Handled = true; } }