4

我的 silverlight 应用程序中有一个切换按钮,如下所示:

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

切换当前正在更改切换时的视觉状态,仅此而已。
但是,我需要用不同的命令绑定每个切换。例如:按下按钮 1,运行command1,再次按下按钮,运行command2

如何才能做到这一点?

4

2 回答 2

11

用于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 包来获取它。

于 2012-08-13T10:45:57.263 回答
2

使用单个命令并跟踪切换状态。

有一个视图模型(最好是一些代码隐藏)并让它使用这两个输入来决定实际需要做什么。

于 2012-08-13T10:23:09.527 回答