Search code examples
wpfrouted-commands

contextsensitive RoutedUICommand.CanExecute, Execute


I have a single RoutedUICommand that can be accessed through various places in the UI. Such as global Keyboardshortcut, Menu, ContextMenu or Button. The code that is to be executed in the RoutedUICommand.CanExecute and RoutedUICommand.Execute methods depends on what UI element was used. How can I achieve this differentiation. I was thinking that I could use the (Can)ExecutedRoutedEventArgs.Source or OrigianlSource but the source is always the same. It is the main Root window. How is this usually achieved? What could I possibly be doing wrong?


Solution

  • Normally you could have different CommandBinding implementations for different 'Targets' - having different behavior for each 'Source' is unusual.

    Could you give an example of what you are trying to do?