0

我正在使用常见的 RelayCommand 类。在可视化树的最上方,我有一些命令与 UserControl.InputBindings 相关联,并带有一些常见的字母快捷方式。我也有一个文本框。但是,当我的 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  
4

0 回答 0