I am attempting to bind an event with Caliburn Micro and I am having some issues getting the right messages to the method. I would like to add the ability to press the 'Enter' key after changing the value in a text box and it execute the same method that the button next to is bound to. However, regardless of which key is pressed, I get the following exceptions:
A first chance exception of type 'System.InvalidCastException' occurred in MyApp.exe
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in WindowsBase.dll
At the suggestion of another, similar question Binding KeyDown Event Silverlight, I've tried using ActionExecutionContext, but to no avail.
Here is the xaml:
<TextBox Name="Threshold"
Margin="5"
Grid.Column="1"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cal:ActionMessage MethodName="ExecuteFilterView">
<cal:Parameter Value="$executionContext"/>
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
And the method:
public void ExecuteFilterView(ActionExecutionContext context)
{
//Do stuff...
}
I understand that I could probably save myself some headaches and simply do a standard event handler in the code behind, but this app is an exercise in MVVM and learning to utilize Caliburn.Micro, so I would like to stick with making this particular approach work.
Am I just trying to send the wrong information from the event? Is my xaml not coded properly to get what I want? Or I have missed something else entirely?
If using keyeventargs as parameter type , and it gives u back null ,then look at this: You propably using
System.Windows.Forms.KeyEventArgs
andKeyEventArgs
, as parameter type is refering to this, try withSystem.Windows.Input.KeyEventArgs
(it works for me).Just threw a test together, both of these work for me:
Using full syntax:
Using CM syntax (prefer this as it's way more readable)
This was the test VM:
Can you post your full code - are you sure you have the framework setup correctly? (have you followed the getting started example?
http://caliburnmicro.codeplex.com/wikipage?title=Basic%20Configuration%2c%20Actions%20and%20Conventions&referringTitle=Documentation
Edit:
Ok after your clarification I can give you some examples of how to do this - I'll tell you my personal preference and why, but choose the one that fits you best
ActionExecutionContext
and casting the eventargs:EventArgs
directlySpecialValues
dictionary entry:In your
Bootstrapper.Configure
method...Your action:
And the code:
The reason this is my favourite? It means that your VM just receives the value you want (most of the time you don't care about a lot of the other parameters) and you don't need to know how to or bother to cast the eventargs - you can just operate on the value. Obviously use whatever is best for you
It's also worth noting, that if you have other types of controls that subclass
KeyEventArgs
this will work for them. If they don't subclassKeyEventArgs
but they still return a value of typeKey
this will still work too as you can just add another cast to the delegate if the first one fails:e.g.