0

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?

Notre
  • 195
  • 3
  • 9
  • Not sure exactly what you need here. So, if your custom check passes, you don't want the Executed handler to be called? Instead you want it to be called on other commands on the tree? And if the command doesn't pass, you want it to be called? – Szymon Rozga Jul 30 '10 at 17:11
  • If the custom check fails. then I don't want the executed handler to be called; instead nothing happens. If the custom check succeeds, then I want to invoke the command as if my previewexecute handler were not in place. – Notre Jul 30 '10 at 21:04

2 Answers2

0

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.

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
0

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);
                }
            }
        }
    }
Community
  • 1
  • 1
Notre
  • 195
  • 3
  • 9