开发者

How to fire a routed command from a specified event?

开发者 https://www.devze.com 2022-12-28 13:14 出处:网络
I\'ve a simple question about commands in WPF : I have a button with an ICommand bound to the Command property, and when开发者_如何学JAVA I click on this button, the command is started, and wow it wor

I've a simple question about commands in WPF : I have a button with an ICommand bound to the Command property, and when开发者_如何学JAVA 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.


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);
    }
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号