Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just threw a test together, both of these work for me:</p> <p>Using full syntax:</p> <pre><code> &lt;TextBox Name="Threshold" Margin="5" Grid.Column="1"&gt; &lt;i:Interaction.Triggers&gt; &lt;i:EventTrigger EventName="KeyDown"&gt; &lt;cal:ActionMessage MethodName="ExecuteFilterView"&gt; &lt;cal:Parameter Value="$executionContext"/&gt; &lt;/cal:ActionMessage&gt; &lt;/i:EventTrigger&gt; &lt;/i:Interaction.Triggers&gt; &lt;/TextBox&gt; </code></pre> <p>Using CM syntax (prefer this as it's way more readable)</p> <pre><code> &lt;TextBox Name="Threshold" Margin="5" Grid.Column="1" cal:Message.Attach="[Event KeyDown] = [Action ExecuteFilterView($executionContext)]" /&gt; </code></pre> <p>This was the test VM:</p> <pre class="lang-cs prettyprint-override"><code>public class MainWindowViewModel { public void ExecuteFilterView(ActionExecutionContext context) { // This method is hit and the context is present and correct } } </code></pre> <p>Can you post your full code - are you sure you have the framework setup correctly? (have you followed the getting started example? </p> <p><a href="http://caliburnmicro.codeplex.com/wikipage?title=Basic%20Configuration%2c%20Actions%20and%20Conventions&amp;referringTitle=Documentation" rel="noreferrer">http://caliburnmicro.codeplex.com/wikipage?title=Basic%20Configuration%2c%20Actions%20and%20Conventions&amp;referringTitle=Documentation</a></p> <p><strong>Edit:</strong></p> <p>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</p> <ol> <li>Using <code>ActionExecutionContext</code> and casting the eventargs:</li> </ol> <pre class="lang-xml prettyprint-override"><code> cal:Message.Attach="[Event KeyDown] = [Action ExecuteFilterView($executionContext)]" </code></pre> <pre class="lang-cs prettyprint-override"><code> public void ExecuteFilterView(ActionExecutionContext context) { var keyArgs = context.EventArgs as KeyEventArgs; if (keyArgs != null &amp;&amp; keyArgs.Key == Key.Enter) { // Do Stuff } } </code></pre> <ol start="2"> <li>Using <code>EventArgs</code> directly</li> </ol> <pre class="lang-xml prettyprint-override"><code> cal:Message.Attach="[Event KeyDown] = [Action ExecuteFilterView($eventArgs)]" </code></pre> <pre class="lang-cs prettyprint-override"><code> public void ExecuteFilterView(KeyEventArgs keyArgs) { if (keyArgs.Key == Key.Enter) { // Do Stuff } } </code></pre> <ol start="3"> <li>My personal fave, creating your own <code>SpecialValues</code> dictionary entry:</li> </ol> <p>In your <code>Bootstrapper.Configure</code> method...</p> <pre class="lang-cs prettyprint-override"><code>MessageBinder.SpecialValues.Add("$pressedkey", (context) =&gt; { // NOTE: IMPORTANT - you MUST add the dictionary key as lowercase as CM // does a ToLower on the param string you add in the action message, in fact ideally // all your param messages should be lowercase just in case. I don't really like this // behaviour but that's how it is! var keyArgs = context.EventArgs as KeyEventArgs; if (keyArgs != null) return keyArgs.Key; return null; }); </code></pre> <p>Your action:</p> <pre><code>cal:Message.Attach="[Event KeyDown] = [Action ExecuteFilterView($pressedKey)]" </code></pre> <p>And the code: </p> <pre class="lang-cs prettyprint-override"><code>public void ExecuteFilterView(Key key) { if (key == Key.Enter) { // Do Stuff } } </code></pre> <p>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</p> <p>It's also worth noting, that if you have other types of controls that subclass <code>KeyEventArgs</code> this will work for them. If they don't subclass <code>KeyEventArgs</code> but they still return a value of type <code>Key</code> this will still work too as you can just add another cast to the delegate if the first one fails:</p> <p>e.g.</p> <pre class="lang-cs prettyprint-override"><code>MessageBinder.SpecialValues.Add("$pressedkey", (context) =&gt; { var keyArgs = context.EventArgs as KeyEventArgs; if (keyArgs != null) return keyArgs.Key; // Ok so it wasn't KeyEventArgs... check for some other type - maybe a 3rd party implementation var thirdPartyKeyArgs = context.EventArgs as ThirdPartyKeyArgs; if (thirdPartyKeyArgs != null) return thirdPartyKeyArgs.KeyProperty; return null; }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload