将命令绑定到 ToggleButton Checked 和 Unchecked 事件 [英] Binding commands to ToggleButton Checked and Unchecked events

查看:45
本文介绍了将命令绑定到 ToggleButton Checked 和 Unchecked 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 C# WPF 应用程序中有一个 ToggleButton,我想将一个命令绑定到 Checked 事件,并将一个 Command 绑定到 Unchecked 事件.

I have a ToggleButton in my C# WPF application where I would like to bind one Command to the Checked event and one Command to the Unchecked event.

我目前拥有的如下:

<ToggleButton Name="btnOpenPort" Style="{StaticResource myOnOffBtnStyle}" Content="Open Port"
              Checked="btnOpenPort_Checked" Unchecked="btnOpenPort_Unchecked"
              IsChecked="{Binding Path=PortViewModel.PortIsOpen, Mode=OneWay}"
              Canvas.Left="75" Canvas.Top="80" Height="25" Width="100"/>

但这不是我的目标.因为在这种情况下,我必须在代码中为 Checked 和 Unchecked 事件设置属性.相反,一旦 Checked 或 Unchecked 事件被触发,我想在我的 ViewModel 中调用一个命令 (ICommand),这样我的切换按钮就不需要任何代码隐藏.

But this is not what I aim to do. Because in this case, I would have to set properties in the code behind for the Checked and Unchecked event. Instead, I would like to call a Command (ICommand) in my ViewModel once the Checked or Unchecked event gets fired so that I don't need any code-behind for my toggle button.

有没有办法在 XAML 中直接为这两个事件绑定命令?类似于WPF中标准"按钮控件的命令属性?

Is there a way to bind a command directly for these two events in XAML? Similar to the command property of the "standard" button control in WPF?

编辑这就是 @har07 提示的工作方式:

EDIT This is how it works with regards to @har07 hint:

1:如果您还没有,请添加参考:

1: Added references if you dont have it yet:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"

2:为选中和未选中事件实现了 Interaction.Triggers:

2: Implemented Interaction.Triggers for Checked and Unchecked events:

<ToggleButton 
        Name="btnOpenPort" Style="{StaticResource myOnOffBtnStyle}" Content="Open Port"
        IsChecked="{Binding Path=PortViewModel.PortIsOpen, Mode=OneWay}"
        Canvas.Left="75" Canvas.Top="80" Height="25" Width="100">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Checked">
                <i:InvokeCommandAction Command="{Binding Path=PortViewModel.OpenPort}"/>
            </i:EventTrigger>
            <i:EventTrigger EventName="Unchecked">
                <i:InvokeCommandAction Command="{Binding Path=PortViewModel.ClosePort}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
</ToggleButton>

使用此解决方案,我无需更改 ViewModel 中的一行代码或我的代码.我可以像使用遵循 MVVM 模式的标准按钮一样调用我的 ICommand.

With this solution, I don't have to change a single line of code in my ViewModel or my code behind. I can just call my ICommand as I would do it with a standard button following MVVM pattern.

推荐答案

你可以把处理checked/unchecked事件的逻辑放在PortIsOpen属性的setter中:

You can put the logic to handle checked/unchecked event in the setter of PortIsOpen property :

private bool _portIsOpen;
public bool PortIsOpen
{
    get { return _portIsOpen; }
    set
    {
        if(value) HandleCheckedEvent();
        else HandleUnCheckedEvent();
        ....
    }
}

或者您可以使用 Ineraction.Triggers 扩展将事件绑定到命令:WPF 将 UI 事件绑定到 ViewModel 中的命令

Or you can use Ineraction.Triggers extension to bind event to commmand : WPF Binding UI events to commands in ViewModel

这篇关于将命令绑定到 ToggleButton Checked 和 Unchecked 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆