简体   繁体   English

如何从指定事件中触发路由命令?

[英]How to fire a routed command from a specified event?

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 :) 关于WPF中的命令,我有一个简单的问题:我有一个按钮,该按钮的ICommand绑定到Command属性,当我单击此按钮时,该命令已启动,并且效果很好:)

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. 现在我想在按下按钮时启动一个特定的命令(我认为是MouseLeftButtonDown事件),但是我不知道如何将多个命令放到一个按钮上,并指定启动命令的事件。

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: 您可能会考虑在Button上建立自己的类,并使用自己的CommandCommandTargetCommandParameter类属性(甚至可能是DependencyProperty扩展它。要触发命令时,只需执行以下操作:

 void FireCommand()
 {
    var routedCommand = Command as RoutedCommand;
    if (routedCommand != null)
    {
       routedCommand.Execute(CommandParameter, CommandTarget);
    }
    else if (Command != null)
    {
       Command.Execute(CommandParameter);
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM