【问题标题】:How to add EventToCommand in XAML如何在 XAML 中添加 EventToCommand
【发布时间】:2025-12-05 01:10:01
【问题描述】:

我使用 Mvvm light(wpf45)。

我想在 XAML 中添加 EventToCommand,我用这个

xmlns:cmd ="http://www.galasoft.ch/mvvmlight"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

但不要添加 EventToCommand

<Button>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
     <cmd:EventToCommand/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>

&lt;cmd:EventToCommand/&gt; 出现错误

【问题讨论】:

  • 您是否忘记添加 i 命名空间和/或设置对交互程序集的引用?
  • 不,我为i 添加了xmlns。我在cmd 中收到错误。

标签: wpf mvvm-light xml-namespaces


【解决方案1】:

我认为你应该这样编码。

<i:Interaction.Behaviors>
 <i:EventToCommand EventName="YourPropertyEvent" Command="YourBinding"/>
</i:Interaction.Behaviors>

【讨论】:

    【解决方案2】:

    使用此参考,

    xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
    

    你可以使用 EventToCommand 类。

    希望对你有所帮助:)

    【讨论】: