views:

465

answers:

3

I have a WPF window, which contains a button called Cancel. Under ordinary circumstances I want a user to be able to press Alt+C to cancel their actions and close the program. As such, the button is captioned "_Cancel."

Problem 1: During Window_Load, if I press C without modifiers, the Cancel_Clicked event fires, and the program closes.

Problem 2: After my program opens, assuming I don't interact with anything on the window, pressing C without modifiers will close the program.

Note that, due to problem 2, using some sort of boolean to track the "loaded" status won't work.

What am I doing wrong? Any ideas?

+1  A: 

I've found that this is the same behavior that is in WinForms. (I was in disbelief as well until I created a test project to try it out!). Here is a previous SO question that hits on the same scenario: http://stackoverflow.com/questions/696973/wpf-accelerator-keys-like-visual-studio

If you want to force the hotkey to ALWAYS be triggered with an Alt modifier, you could consider refactoring the action into a command.

Here is a very quick and hacky demonstration on how one might go about implementing such a command.

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
}
Michael Swart
+1  A: 

Your best solution is to refactor this into a command:

<Window.CommandBindings>
  <CommandBinding Command="Cancel" Executed="CancelExecuted" />
</Window.CommandBindings>

<Window.InputBindings>
  <KeyBinding Command="Cancel" Key="C" Modifiers="Alt"/>
</Window.InputBindings>
Will Eddins