Note that there are some explanatory texts on larger screens.

plurals
  1. POMVVM-Light EventToCommand Behavior for CheckBox Checked/Unchecked in Silverlight
    text
    copied!<p>I would like to handle the Checked and Unchecked events of a Checkbox control and execute a command in my ViewModel. I wired up an EventTrigger for both the Checked and Unchecked events as follows:</p> <pre><code>&lt;CheckBox x:Name="chkIsExtendedHr" IsChecked="{Binding Schedule.Is24Hour, Mode=TwoWay}"&gt; &lt;i:Interaction.Triggers&gt; &lt;i:EventTrigger EventName="Checked"&gt; &lt;GalaSoft_MvvmLight_Command:EventToCommand CommandParameter="{Binding IsChecked, ElementName=chkIsExtendedHr}" Command="{Binding Path=SetCloseTime, Mode=OneWay}" /&gt; &lt;/i:EventTrigger&gt; &lt;i:EventTrigger EventName="Unchecked"&gt; &lt;GalaSoft_MvvmLight_Command:EventToCommand CommandParameter="{Binding IsChecked, ElementName=chkIsExtendedHr}" Command="{Binding Path=SetCloseTime, Mode=OneWay}" /&gt; &lt;/i:EventTrigger&gt; &lt;/i:Interaction.Triggers&gt; &lt;/CheckBox&gt; </code></pre> <p>I defined a RelayCommand in my ViewModel and wired up an action for it:</p> <pre><code>public RelayCommand&lt;Boolean&gt; SetCloseTime{ get; private set; } ... SetCloseTime= new RelayCommand&lt;bool&gt;(ExecuteSetCloseTime); </code></pre> <p>The parameter in the action for the command always resolves to the previous state of the CheckBox, e.g. false when the CheckBox is checked, and true when the CheckBox is unchecked.</p> <pre><code>void ExecuteSetCloseTime(bool isChecked) { if (isChecked) { // do something } } </code></pre> <p>Is this expected behavior?</p> <p>I have a workaround where I have separate triggers (and commands) for the Checked and Unchecked and use a <code>RelayCommand</code> instead of <code>RelayCommand&lt;bool&gt;</code>. Each command executes correctly when the CheckBox is checked and unchecked. Feels a little dirty though - even dirtier than having UI code in my ViewModel :)</p> <p>Thanks</p>
 

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