且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以编程方式发出命令

更新时间:2023-11-30 13:59:16

不确定您的意思是

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

在c#6及更高版本(由eirik提出)中,有一种缩写形式:

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

Mybutton.Command?.Execute(null);

更新
正如@Benjol所建议的那样,在某些情况下可能需要提供按钮的 CommandParameter -property值,并且可以将其添加而不是 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 null may be considered to be used as the default pattern:

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