Binding a toggle button to two commands

I have a toggle button in a silverlight application as follows:

<ToggleButton Content="ToggleButton" Margin="0,30,0,0" Style="{StaticResource ChkToggleButton}" /> 

Currently, the switch toggles the visual states when switching and nothing else.
However, I need to associate each of the switches with a different command. For example, if you click "On" 1, run command1 and press the β€œON” button again, Run command2 .

How can I do that?

+4
source share
2 answers

Use Triggers for these purposes:

 <ToggleButton Content="ToggleButton" Margin="0,30,0,0" Style="{StaticResource ChkToggleButton}"> <i:Interaction.Triggers> <i:EventTrigger EventName="Checked"> <i:InvokeCommandAction Command="{Binding FirstCommand}" CommandParameter="{Binding SomeParameter}" /> </i:EventTrigger> <i:EventTrigger EventName="Unchecked"> <i:InvokeCommandAction Command="{Binding SecondCommand}" CommandParameter="{Binding AnotherParameter}" /> </i:EventTrigger> </i:Interaction.Triggers> </ToggleButton> 

If you do not have Interactivity , you can get it by installing the Expression Blend SDK or as a NuGet package .

+9
source

use one command and also monitor the switching status.

You have a viewmodel (preferably, or some codebehind), and let it use these two inputs to decide what actually needs to be done.

+2
source

All Articles