我的KeyBindings正在吃TextBox中的按键

时间:2013-09-04 20:02:24

标签: c# wpf data-binding routed-commands

我正在使用常见的RelayCommand类。在可视树中,我有许多与UserControl.InputBindings绑定的命令,带有一些常见的字母快捷方式。我也有一个TextBox。但是,当我的TextBox具有焦点并按下任何绑定到命令的键时,该命令将执行,并且该字符永远不会显示在TextBox中。我原以为聚焦元素会有第一次机会使用密钥并将其标记为已处理。为什么我的命令首先被触发?我希望它只在没有其他输入控制进一步向下处理事件时触发。

更新:关键代码似乎位于CommandManager.TranslateInput的底部。在这里拆解:

  bool continueRouting = false;
  RoutedCommand routedCommand = command as RoutedCommand;
  if (routedCommand != null)
  {
    if (routedCommand.CriticalCanExecute(parameter, target, inputEventArgs.UserInitiated, out continueRouting))
    {
      continueRouting = false;
      CommandManager.ExecuteCommand(routedCommand, parameter, target, inputEventArgs);
    }
  }
  else if (command.CanExecute(parameter))
    command.Execute(parameter);
  inputEventArgs.Handled = !continueRouting;

无法使用ICommand并且Handled不会返回true。我唯一的选择是让TextBox在进入UIElement.OnKeyDownThunk之前处理键输入,UIElement.OnKeyDownThunk直接调用CommandManager。或者除了InputBinding之外还有其他方法可以触发RelayCommand吗?这是堆栈跟踪:

>   Asi.ViewModelShared.dll!Asi.ViewModelShared.RelayCommand.Execute(object parameter) Line 50  C#
    PresentationCore.dll!System.Windows.Input.CommandManager.TranslateInput(System.Windows.IInputElement targetElement, System.Windows.Input.InputEventArgs inputEventArgs) + 0x5c5 bytes   
    PresentationCore.dll!System.Windows.UIElement.OnKeyDownThunk(object sender, System.Windows.Input.KeyEventArgs e) + 0x52 bytes   
    PresentationCore.dll!System.Windows.Input.KeyEventArgs.InvokeEventHandler(System.Delegate genericHandler, object genericTarget) + 0x2c bytes    
    PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) + 0x33 bytes  
    PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target, System.Windows.RoutedEventArgs routedEventArgs) + 0x44 bytes    
    PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source, System.Windows.RoutedEventArgs args, bool reRaised) + 0x1a8 bytes  
    PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender, System.Windows.RoutedEventArgs args) + 0x73 bytes  
    PresentationCore.dll!System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs args) + 0x3d bytes   
    PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args, bool trusted) + 0x40 bytes    
    PresentationCore.dll!System.Windows.Input.InputManager.ProcessStagingArea() + 0x1f8 bytes   
    PresentationCore.dll!System.Windows.Input.InputManager.ProcessInput(System.Windows.Input.InputEventArgs input) + 0x45 bytes 
    PresentationCore.dll!System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport inputReport) + 0x62 bytes  
    PresentationCore.dll!System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(System.IntPtr hwnd, System.Windows.Input.InputMode mode, int timestamp, System.Windows.Input.RawKeyboardActions actions, int scanCode, bool isExtendedKey, bool isSystemKey, int virtualKey) + 0xee bytes 
    PresentationCore.dll!System.Windows.Interop.HwndKeyboardInputProvider.ProcessKeyAction(ref System.Windows.Interop.MSG msg, ref bool handled) + 0xac bytes   
    PresentationCore.dll!System.Windows.Interop.HwndSource.CriticalTranslateAccelerator(ref System.Windows.Interop.MSG msg, System.Windows.Input.ModifierKeys modifiers) + 0x94 bytes   
    PresentationCore.dll!System.Windows.Interop.HwndSource.OnPreprocessMessage(object param) + 0x12c bytes  

0 个答案:

没有答案