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:
<CheckBox x:Name="chkIsExtendedHr" IsChecked="{Binding Schedule.Is24Hour, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<GalaSoft_MvvmLight_Command:EventToCommand
CommandParameter="{Binding IsChecked, ElementName=chkIsExtendedHr}"
Command="{Binding Path=SetCloseTime, Mode=OneWay}" />
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<GalaSoft_MvvmLight_Command:EventToCommand
CommandParameter="{Binding IsChecked, ElementName=chkIsExtendedHr}"
Command="{Binding Path=SetCloseTime, Mode=OneWay}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
I defined a RelayCommand in my ViewModel and wired up an action for it:
public RelayCommand<Boolean> SetCloseTime{ get; private set; }
...
SetCloseTime= new RelayCommand<bool>(ExecuteSetCloseTime);
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.
void ExecuteSetCloseTime(bool isChecked)
{
if (isChecked)
{
// do something
}
}
Is this expected behavior?
I have a workaround where I have separate triggers (and commands) for the Checked and Unchecked and use a RelayCommand
instead of RelayCommand<bool>
. 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 :)
Thanks