1

I've a simple question about commands in WPF : I have a button with an ICommand bound to the Command property, and when I click on this button, the command is started, and wow it works :)

Now I whant to start a particular command when I'm just pushing down the button (MouseLeftButtonDown event I think), but I don't know how can I put multiple commands to one button, and specify the event who will start the command.

Do you have any idea ? Maybe a custom control ?

Thanks for you help,
Antoine.

Antoine Blanchet
  • 325
  • 2
  • 17

1 Answers1

3

You might consider basing your own class on Button and extending it with your own set of Command, CommandTarget and CommandParameter-like properties (possibly even DependencyProperty. When you want to fire the command, just do this:

 void FireCommand()
 {
    var routedCommand = Command as RoutedCommand;
    if (routedCommand != null)
    {
       routedCommand.Execute(CommandParameter, CommandTarget);
    }
    else if (Command != null)
    {
       Command.Execute(CommandParameter);
    }
 }
wpfwannabe
  • 14,587
  • 16
  • 78
  • 129
  • Ok, so I'll create a custom control based on a button, with some new dependecy property that can get ICommand, and when the event MouseLeftButtonDown is fired I start the right command like you wrote ... Thank you :) – Antoine Blanchet Apr 30 '10 at 13:11
  • After reading your code I realized the problem I was having in my own project. I didn't realize that RoutedCommand doesn't implement the ICommand.Execute method in a routed fashion. This makes sense when I thinking about it now, the command has no knowledge of where itself lies within the visual tree and therefore needs the second parameter to tell you where to start routing from. – jpierson Dec 09 '10 at 20:30