Caliburn.Micro Action with parameter not binding
Asked Answered
P

1

5

I have a textbox with the following:

<i:Interaction.Triggers>
 <i:EventTrigger EventName="KeyUp" >                                               
     <cal:ActionMessage MethodName="OnKeyUp" >
          <cal:Parameter Value="$eventArgs"/>
     </cal:ActionMessage>
 </i:EventTrigger>

If I run this, an error message is produced saying "No target found for Method OnKeyUp." If I remove the parameter from the message, and the method, then it runs fine.

This is the method.

public void OnKeyUp(object sender, KeyEventArgs e) {
        MessageBox.Show(e.Key.ToString());
    }

I don't see what the issue is.

Puritanism answered 27/4, 2013 at 17:46 Comment(0)
S
11

Your view model method takes two parameters, but you're only passing one.

Either change your view to pass the $source:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="KeyUp">
        <cal:ActionMessage MethodName="OnKeyUp" >
           <cal:Parameter Value="$source" />
           <cal:Parameter Value="$eventArgs" />
        </cal:ActionMessage>
    </i:EventTrigger>
</i:Interaction.Triggers>

or change your method to just take the event arguments:

public void OnKeyUp(KeyEventArgs e) { ... }

You could also use the much nicer shorthand:

<TextBox cal:Message.Attach="[Event KeyUp] = [Action OnKeyUp($source, $eventArgs)]" />
Subcartilaginous answered 27/4, 2013 at 18:25 Comment(2)
OK, I see the issue now. Thanks very much.Puritanism
how can I access specific property in source? for example I try $source:Document but it throws exception and says value Document was not found. but Document is a property of source.Murraymurre

© 2022 - 2024 — McMap. All rights reserved.