tags:

views:

20

answers:

2

Let's imagine I have XAML that looks like this:

<UserControl.CommandBindings>
  <CommandBinding Command="ApplicationCommands.Delete" Executed="CommandBinding_DeleteExecuted" PreviewExecuted="CommandBinding_PreviewDeleteExecuted"/>
</UserControl.CommandBindings>

And I have C# code that looks like this:

private void CommandBinding_DeleteExecuted(object sender, ExecutedRoutedEventArgs e)
{

}

private void CommandBinding_PreviewDeleteExecuted(object sender, ExecutedRoutedEventArgs e)
{
  //Would like to invoke Delete command from here
}

What I would like to do is have my preview handler get called and do some custom check. If the custom check passes, then I would like to invoke the delete command again, from within the preview handler (or rather than invoking, if I could have the original command pass 'through' my preview and execute handlers, that would be great). So, at this point, when I re-issue the delete command, no further XAML is involved.

If I reissue the Delete command from my preview handler somehow, I imagine it would result in the preview handler being called again (endlessly in a loop). Instead, I would like to have the re-issued command ignore my preview and execute handler and let any delete commands handler further down in the tree handle the delete command.

Is there any way to do this?

A: 

There isn't - the command handler will always end up hitting the parent first; if you want to proxy one command to another, they have to have different names.

Paul Betts
A: 

I have a solution based on another question that seems to be working okay:

    private void CommandBinding_PreviewDeleteExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        bool ignoreDeleteCommand = DoSomeTest();


        if (!ignoreDeleteCommand)
        {
            foreach (CommandBinding cb in CommandBindings)
            {
                if (cb.Command.Equals(ApplicationCommands.Delete))
                {
                    //Unsubscribe from this handler, invoke the Delete command (which will be handled by child) and then
                    //resubscribe to this handler
                    cb.PreviewExecuted -= new ExecutedRoutedEventHandler(CommandBinding_PreviewDeleteExecuted);
                    cb.Command.Execute(null);
                    cb.PreviewExecuted += new ExecutedRoutedEventHandler(CommandBinding_PreviewDeleteExecuted);
                }
            }
        }
    }
Notre