我发现这与 WinForms 中的行为相同。(直到我创建了一个测试项目来尝试它之前,我都不敢相信!)。这是之前遇到的相同场景的 SO 问题:WPF Accelerator Keys like Visual Studio
如果您想强制使用 Alt 修饰符始终触发热键,您可以考虑将操作重构为命令。
这是一个关于如何实现这样一个命令的非常快速和 hacky 的演示。
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.InputBindings.Add(new KeyBinding(new DoActionCommand(() => DoIt()), new KeyGesture(Key.C, ModifierKeys.Alt)));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
DoIt();
}
private void DoIt()
{
MessageBox.Show("Did It!");
}
}
public class DoActionCommand : ICommand
{
private Action _executeAction;
public DoActionCommand(Action executeAction)
{
_executeAction = executeAction;
}
#region ICommand Members
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
if (_executeAction != null)
{
_executeAction.Invoke();
}
}
#endregion
}