I\'m building an application using the MVVM design pattern and I want to make use of the RoutedUICommands defined in the ApplicationCommands class. Since the CommandBindings
EDIT 2: From looking at the source code it seems that internally it works like that:
- The
UIElement
callsCommandManager.TranslateInput()
in reaction to user input (mouse or keyboard).- The
CommandManager
then goes throughCommandBindings
on different levels looking for a command associated with the input.- When the command is found its
CanExecute()
method is called and if it returnstrue
theExecuted()
is called.- In case of
RoutedCommand
each of the methods does essencially the same thing - it raises a pair of attached eventsCommandManager.PreviewCanExecuteEvent
andCommandManager.CanExecuteEvent
(orPreviewExecutedEvent
andExecutedEvent
) on theUIElement
that initiated the process. That concludes the first phase.- Now the
UIElement
has class handlers registered for those four events and these handlers simply callCommandManager.OnCanExecute()
andCommandManager.CanExecute()
(for both preview and actual events).- It is only here in
CommandManager.OnCanExecute()
andCommandManager.OnExecute()
methods where the handlers registered withCommandBinding
are invoked. If there are none found theCommandManager
transfers the event up to theUIElement
's parent, and the new cycle begins until the command is handled or the root of the visual tree is reached.
If you look at the CommandBinding class source code there is OnExecuted() method that is responsible for calling the handlers you register for PreviewExecuted and Executed events through CommandBinding. There is that bit there:
PreviewExecuted(sender, e);
e.Handled = true;
this sets the event as handled right after your PreviewExecuted handler returns and so the Executed is not called.
EDIT 1: Looking at CanExecute & PreviewCanExecute events there is a key difference:
PreviewCanExecute(sender, e); if (e.CanExecute) { e.Handled = true; }
setting Handled to true is conditional here and so it is the programmer who decides whether or not to proceed with CanExecute. Simply do not set the CanExecuteRoutedEventArgs's CanExecute to true in your PreviewCanExecute handler and the CanExecute handler will be called.
As to
ContinueRouting
property of Preview event - when set to false it prevents the Preview event from further routing, but it does not affect the following main event in any way.
Note, that it only works this way when handlers are registered through CommandBinding.
If you still want to have both PreviewExecuted and Executed to run you have two options:
Execute()
method of the routed command from within PreviewExecuted handler. Just thinking about it - you might run into sync issues as you're calling Executed handler before the PreviewExecuted is finished. To me this doesn't look like a good way to go.CommandManager.AddPreviewExecutedHandler()
static method. This will be called directly from UIElement class and will not involve CommandBinding. EDIT 2: Look at the point 4 at the beginning of the post - these are the events we're adding the handlers for.
From the looks of it - it was done this way on purpose. Why? One can only guess...