Executed not called after PreviewExecuted

Here's my code:

var commandBinding = new CommandBinding(ApplicationCommand.New);
commandBinding.PreviewExecuted += OnPreviewExecuted;
commandBinding.Executed += OnExecuted;

void OnPreviewExecuted(object sender, ExecutedRoutedEventArgs e) {
  e.Handled = false;

void OnExecuted(object sender, ExecutedRoutedEventArgs e) {


MSDN says "... If the preview event is not handled, the Executed event is raised on the target command."

This works correctly for the PreviewCanExecute event. But in this case, Executed-Event will not be called while listening to PreviewExecuted-Event.

I haven't found anything around this topic, so I want to ask if this behavior is or is just wrong.


source to share

1 answer

It doesn't seem to matter what you set e.Handled


Here is the code that decides which events to raise (ExecuteImpl in RoutedCommand.cs):

ExecutedRoutedEventArgs args = new ExecutedRoutedEventArgs(this, parameter);
args.RoutedEvent = CommandManager.PreviewExecutedEvent;

if (targetUIElement != null)
    targetUIElement.RaiseEvent(args, userInitiated);

if (!args.Handled)
    args.RoutedEvent = CommandManager.ExecutedEvent;
    if (targetUIElement != null)
        targetUIElement.RaiseEvent(args, userInitiated);


So, if it e.Handled

is false

after a preview event, the event Executed

should be raised. But this will never be false after calling the handler PreviewExecuted

(CommandBindings.cs, OnExecuted):

PreviewExecuted(sender, e);
e.Handled = true;


It just sets e.Handled

to true after calling the preview handler ...

Why this is so, I have no idea. PreviewCanExecute

works the same, but it only sets e.Handled

to true if e.CanExecute

set to true - if you do this in your preview handler, the handler CanExecute

will not be called regardless e.Handled


My guess is that "If the preview event is not handled" is the unfortunate wording of "If the preview event has no handler registered ".



All Articles