wpf 以编程方式发出命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3520713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:02:15  来源:igfitidea点击:

Programmatically raise a command

wpfcommand

提问by Jefim

I have a button:

我有一个按钮:

<Button x:Name="MyButton" Command="SomeCommand"/>

Is there a way to execute the command from source? Calling the click on the button does not help:

有没有办法从源代码执行命令?调用单击按钮无济于事:

MyButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));

I mean - this does raise the event, but it does not raise the command. Is there something similar to this RaiseEventbut just for Command? If there is not - how can I instantiate ExecutedRoutedEventArgs? Is it possible?

我的意思是 - 这确实会引发事件,但不会引发命令。有没有类似的东西,RaiseEvent但只是为了命令?如果没有 - 我如何实例化ExecutedRoutedEventArgs?是否可以?

Lastly - please do not tell me how to avoidcalling the command.

最后 - 请不要告诉我如何avoid调用命令。

回答by HCL

Not sure if you mean:

不确定你的意思是:

if(MyButton.Command != null){
    MyButton.Command.Execute(null);
}

with c#6 and later (as proposed by eirik) there is the short form:

使用 c#6 及更高版本(由 eirik 提出)有简短的形式:

Mybutton.Command?.Execute(null);

Update
As proposed by @Benjol, providing the button's CommandParameter-property value can be required in some situations and the addition of it instead of nullmay be considered to be used as the default pattern:

更新
正如@Benjol 所提议的,CommandParameter在某些情况下可能需要提供按钮的-property 值,并且添加它而不是null可以被认为用作默认模式:

Mybutton.Command?.Execute(MyButton.CommandParameter);

回答by Dominic

Or if you don't have access to the UI element you can call the static command directly. For example I have a system wide key hook in App.xamland wanted to call my play/pause/stop etc media events:

或者,如果您无权访问 UI 元素,则可以直接调用静态命令。例如,我有一个系统范围的关键钩子App.xaml,想调用我的播放/暂停/停止等媒体事件:

CustomCommands.PlaybackPlayPause.Execute(null, null);

passing 2nd parameter as null will call all attached elements.

将第二个参数传递为 null 将调用所有附加元素。

回答by Eric Ouellet

My prefered way to do it is to do as Sean Sexton recommend in Executing a Command Programmatically

我更喜欢的方法是按照 Sean Sexton 在Executing a Command Programmatically 中推荐的那样做

In short, find the command, check if it can execute and if so, execute.

简而言之,找到命令,检查它是否可以执行,如果可以,则执行。

Example:

例子:

    if (ApplicationCommands.Open.CanExecute(null, null))
        ApplicationCommands.Open.Execute(null, null);

Why I think it is better:I think it's the best way because it will really use the proper path and you do not depends on naming any control. Also, although you know now that you don't use "CanExecute", you never know when somebody will add a behavior for it in the future.

为什么我认为它更好:我认为这是最好的方法,因为它将真正使用正确的路径,并且您不依赖于命名任何控件。此外,虽然您现在知道您不使用“CanExecute”,但您永远不知道将来何时有人会为其添加行为。

回答by BlueCode

You need ICommand.Execute(object)to accomplish that.

你需要ICommand.Execute(object)做到这一点。

Working example for your sample code: this.MyButton.Command.Execute(null);

示例代码的工作示例:this.MyButton.Command.Execute(null);