c# - 将切换按钮绑定(bind)到两个命令

标签 c# wpf silverlight xaml

我的 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/

相关文章:

c# - 运行(一次)协方差计算

c# - OnClient Click 使用 Freetextbox 的 javascript

wpf - 如何在Blend/VS中获得所见即所得的设计,而没有在每个XAML文件中合并ResourceDictionary?

c# - 展开最后一个网格行以填充 wpf 应用程序中的窗口

wpf - XAML 样式的在线资源

c# - 将外部 .exe 烘焙到 C# 项目中

c# - 不允许 Enter Key 在 MultiLine TextBox C# 中换行

c# - 从命令操作中更新 UI 元素

silverlight - 显示 Silverlight 生成的消息

c# - 磁贴在 WP8.1 Silverlight 应用程序中未正确显示