我的 silverlight 应用程序中有一个切换按钮,如下所示:
<ToggleButton Content="ToggleButton" Margin="0,30,0,0" Style="{StaticResource ChkToggleButton}" />
当前的切换只是改变切换时的视觉状态,仅此而已。
但是,我需要用不同的命令绑定(bind)每个切换。例如:按下按钮 1 时,运行 command1
,再次按下按钮时,运行 command2
。
如何做到这一点?
最佳答案
使用Triggers
出于此类目的:
<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>
如果您没有Interactivity
,您可以通过安装Expression Blend SDK 来获得它。或作为 NuGet package .
关于c# - 将切换按钮绑定(bind)到两个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11932209/