2012-05-21 12 views
3

基本上我在我的自定義類中有一個事件。我將使用事件的參數 - >屬性作爲該方法的參數調用自定義類中的特定方法。如何在交互中將事件參數作爲參數傳遞。使用MVVM時觸發?

您可以觀察此信息背後的實際代碼。

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

但是我想通過interactive.Triggers在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.XXproperty和e.YYproperty在上述行動中執行我的自定義類中的方法?

回答

1

你可以嘗試使用也互動庫,那麼你可以這樣寫:

<i:EventTrigger EventName="FileOpening"> 
    <ei:CallMethodAction TargetObject="{Binding}" MethodName="OnFileOpening"/> 
</i:EventTrigger> 

而且在你的代碼就會像

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

爲了增加這個答案: 的「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> 
相關問題