Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to pass a parameter (or multiple params) to the CallMethodAction behavior?

Is there a way to pass a parameter (or multiple params) to the CallMethodAction behavior?

like image 838
Shimmy Weitzhandler Avatar asked Feb 22 '11 01:02

Shimmy Weitzhandler


2 Answers

Try InvokeCommandAction a command instead of using CallMethodAction:

<i:Interaction.Triggers>
  <i:EventTrigger EventName="TextChanged">
    <i:InvokeCommandAction Command="{Binding TextChangedCommand}" 
        CommandParameter="{Binding ElementName=filterBox, Path=Text}"/>
  </i:EventTrigger>
</i:Interaction.Triggers>

Hope it helps

like image 113
kruvi Avatar answered Jan 02 '23 17:01

kruvi


After some decompiling it turns out that CallMethodAction does support calling methods with parameters. However, CallMethodAction is very strict on the expected signature. Methods must conform to the following:

public void SomeMethod(object sender, EventArgs args) {
  // do something
}

Where the args parameter can be a subclass of EventArgs, which therefore allows passing in (any number of) custom parameters. For instance:

public class MyEventArgs : EventArgs {

     public MyEventArgs(MyParam param) {
         Param = param;
     }

     MyParam Param { get; private set; }
}

Thus allowing for the following signature:

public void SomeMethod(object sender, MyEventArgs args) {
      var param = args.Param;
      // do something
}    

For reference, here's the code in CallMethodAction that performs the conformity check:

  private static bool AreMethodParamsValid(ParameterInfo[] methodParams)
  {
      if (methodParams.Length == 2)
      {
        if (methodParams[0].ParameterType != typeof(object) || !typeof (EventArgs).IsAssignableFrom(methodParams[1].ParameterType))
            return false;
        }
        else if (methodParams.Length != 0)
          return false;
      return true;
  }
like image 41
Jonas Chapuis Avatar answered Jan 02 '23 16:01

Jonas Chapuis