WPF Application wide command RRS feed

  • Question

  • I've created my own Command implementing ICommand.  I want this command to fire anywhere in the application what the user hits the KeyGesture just like the built in ApplicationCommands.  How can I register my command so that it will work application wide?  The examples I've found so far only show how to do this in one Window at a time, ie:
    <KeyBinding Command="Foo" Key="F2"/>
    How can I set this once and have it everywhere?

    Sunday, July 13, 2008 12:40 AM


  • CommandBinding can only be used in per visual tree basis rather than in application basis, if you want to enable Command/KeyGesture binding at the application level, you need to manually interpret the raw win32 messages from ComponentDispatcher.ThreadPreprocessMessage handler, and inside the handler, you could examine which keystroke has been hit, and manually call ICommand.Execute() method to execute a specified command.

    Hope this helps
    • Marked as answer by Marco Zhou Friday, July 18, 2008 9:50 AM
    Tuesday, July 15, 2008 7:28 AM