【问题标题】:Keyboard command binding on WPF Inside User ControlWPF内部用户控件上的键盘命令绑定
【发布时间】:2013-01-09 11:13:58
【问题描述】:

我正在尝试使用以下代码在 WPF 窗口上绑定键盘命令:

  RoutedCommand cmndSettings = new RoutedCommand();
  cmndSettings.InputGestures.Add(new KeyGesture(Key.S, ModifierKeys.Alt));
  CommandBindings.Add(new CommandBinding(cmndSettings, mnuSettings_Click));

  private void mnuSettings_Click(object sender, ExecutedRoutedEventArgs e)
  {
      MessageBox.Show("key pressed");
  }

如果我将此代码放在 xaml Window 的 cs 文件中,这很好。但是,如果我将它放在用户控件的 cs 文件中,该文件在父窗口中动态加载,则根本不会触发 键盘事件。 我需要做什么才能让它在用户控件中工作?谢谢。

【问题讨论】:

  • UserControl 是否可聚焦?你检查过它真的有焦点吗?尝试在点击或加载时设置焦点。
  • 哦,是的。这就是问题所在。感谢您的了解。

标签: c# wpf events


【解决方案1】:

要在用户控件下使用控件,容器/用户控件能够正确聚焦非常重要。否则,可能无法正常工作。

【讨论】:

    猜你喜欢
    • 2011-08-18
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多