WPF 命令与事件触发器命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13744819/
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-13 06:31:00  来源:igfitidea点击:

WPF Commands vs. event trigger commands

wpfbuttoneventtriggericommand

提问by shahar eldad

In a WPF Buttonwe have a Commandparameter which can be binded to ICommand.

在 WPF 中,Button我们有一个Command可以绑定到ICommand.

<Button Command="{Binding SomeCommand}"/>

We can also use EventTriggerswith InvokeCommandActionto fire a ICommand.

我们也可以使用EventTriggerswithInvokeCommandAction来触发一个ICommand.

<Button>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <i:InvokeCommandAction Command="{Binding SomeCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

What is the difference between them and when to use which?

它们之间有什么区别以及何时使用哪个?

Update:

更新:

I have noticed difference in the following scenario:

我注意到以下场景中的差异:

  • I have a textbox which validates using IValudationRule if the textbox is empty.
  • I added MultiDataTrigger condition to have the IsEnabled property of a save button to be set to false when the Validation.HasError equals to true.
  • 我有一个文本框,如果文本框为空,它会使用 IValudationRule 进行验证。
  • 我添加了 MultiDataTrigger 条件,以便在 Validation.HasError 等于 true 时将保存按钮的 IsEnabled 属性设置为 false。

Using the Button Command all works good, but using the EventTrigger it doesn`t work.

使用 Button Command 一切正常,但使用 EventTrigger 则行不通。

Any reason for this?

这有什么原因吗?

采纳答案by Arsen Mkrtchyan

Snippets you provide are almost the same,if you don't use CanExecute. InvokeCommandActionis not native WPFclass, it is created in Interactionlibrary for the cases when control doesn't provide Commandand you have to bind Command to some event. for example when you need command on ListBox.SelectionChangedor etc.

您提供的代码段几乎相同,如果您不使用CanExecute. InvokeCommandAction不是本机WPF类,它是在Interaction库中创建的,用于不提供控件Command并且您必须将 Command 绑定到某个事件的情况。例如,当您需要命令时ListBox.SelectionChanged等等。

So based on above, my suggestion is, always use Command if it is possible, and use EventTriggeronly when you can't go without it.

因此,基于上述,我的建议是,如果可能,请始终使用 Command,并且EventTrigger仅在您离不开它时使用。

Also Note, than ICommandalso provide CanExecutebased of which button can enable/disable, which will not work in second case

另请注意,ICommand还提供CanExecute基于哪个按钮可以启用/禁用,这在第二种情况下不起作用

回答by m-y

There is a slight difference (CanExecute), but other than that, it's just a matter of what part of the code is subscribing you to the event/command. An ICommandexposes the Executeand CanExecutemethods, so...

有一点不同(CanExecute),但除此之外,这只是代码的哪一部分为您订阅事件/命令的问题。AnICommand暴露了ExecuteCanExecute方法,所以......

ButtonBase's Command property will automatically relay Click events to the command's Execute event and change it's disabled/endabled property based on the command's CanExecute being raised... under the hood (you don't have to worry about wireup).

ButtonBase的 Command 属性将自动将 Click 事件中继到命令的 Execute 事件,并根据正在引发的命令的 CanExecute 更改它的禁用/已结束属性......在幕后(您不必担心接线)。

The Interactionlibrary does the same thing, but exposes various classes to allow you to "build" your own wireup in a simple fashion. You're basically creating a couple of classes that say "Wire the Event Name(ButtonBase.Click) event to call the ICommand.Execute method of the Specified Command(SomeCommand)."

Interaction库做同样的事情,但公开了各种类,以允许您以简单的方式“构建”自己的接线。您基本上是在创建几个类,这些类表示“连接事件名称(ButtonBase.Click) 事件以调用指定命令(SomeCommand)的 ICommand.Execute 方法”。

In fact, if you forgo both of these options you could even roll your own in code-behind... but then again, there is no point in that (other than to learn how it works) when it is offered in a nice, clean, under-the-hood way, unit-tested, optimized way?

事实上,如果你放弃这两个选项,你甚至可以在代码隐藏中推出你自己的选项......但话又说回来,当它以一个很好的方式提供时(除了了解它的工作原理)没有任何意义,干净的、底层的方式、单元测试的、优化的方式?