Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding toggle button to two commands

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

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

The toggle currently is changing visual states on toggle and nothing more.
However, I need to bind each of the toggle with a different command. For eg: On button press1, run command1 and on button press again, run command2.

How can this be done?

like image 361
user1240679 Avatar asked Dec 12 '22 23:12

user1240679


2 Answers

Use Triggers for such 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 don't have Interactivity you can get it by installing Expression Blend SDK or as a NuGet package.

like image 114
Anatolii Gabuza Avatar answered Dec 31 '22 12:12

Anatolii Gabuza


use a single command and also keep track of the toggle state.

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

like image 34
PeteH Avatar answered Dec 31 '22 10:12

PeteH