简体   繁体   中英

Keyboard command binding on WPF Inside User Control

I am trying witth the following code for keyboard command binding on WPF window:

  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");
  }

This is working fine if I place this code inside a xaml Window's cs file. But, if I place it inside a user control's cs file, which dynamically loaded in a parent window, keyboard events aren't triggering there at all. What I need to do to get it working inside a user control plz? Thanks.

To work with controls under a user control, its very important that, the containers/user control is focus-able properly. otherwise, may not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM