如何在使用MVVM时将事件参数作为参数传递给interaction.Trigger?

3

基本上,我在我的自定义类中有一个事件。我将使用事件的参数——属性作为该方法的参数来调用自定义类中的特定方法。

您可以观察此信息背后的实际代码。

instance.FileOpening += (sender, e) =>
                {
                    CustomClass.Method(e.XXproperty, e.YYproperty);
                };

但我希望通过交互来实现这一点。MVVM中的触发器。因此我在XAML中使用了以下代码。

<i:Interaction.Triggers>
     <i:EventTrigger EventName="FileOpening">
          <i:FileOpeningAction TargetObject="{Binding ElementName=cntrol}"/>
     </i:EventTrigger>
</i:Interaction.Triggers>

我的对应的 TargetedTriggerAction 类在这里,用于使我的自定义类执行该方法。
public class FileOpeningAction :TargetedTriggerAction<CustomClass>
    {
        protected override void Invoke(object parameter)
        {
            ((instance).TargetObject).Method(?,?);
        }
    }

但我的问题是如何将上述操作中的 e.XX 属性和 e.YY 属性传递到我的自定义类中执行方法?
2个回答

1
你可以尝试使用交互式库,然后写出这样的代码:
<i:EventTrigger EventName="FileOpening">
    <ei:CallMethodAction TargetObject="{Binding}" MethodName="OnFileOpening"/>
</i:EventTrigger>

在你的代码中,它会类似于这样:

public void OnFileOpening(object sender, EventArgs e){//your code}

补充此答案: “ei”命名空间是“http://schemas.microsoft.com/expression/2010/interactions”,您需要引用Microsoft.Expression.Interactions。 - gbieging

0
如果按照以下方式操作,您可以通过设置“PassEventArgsToCommand”将事件参数传递给命令。
添加引用:
xmlns:cmd="xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4""

<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseEnter" >
         <cmd:EventToCommand Command="{Binding FooCommand}"
             PassEventArgsToCommand="True" />
    </i:EventTrigger>
</i:Interaction.Triggers>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接