将命令绑定到ToggleButton Checked和Unchecked事件 [英] Binding commands to ToggleButton Checked and Unchecked events
问题描述
我在C#WPF应用程序中有一个ToggleButton,我想将一个命令绑定到Checked事件,将一个命令绑定到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中调用Command(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?
EDIT
可以与@ 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或我的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.
推荐答案
在 PortIsOpen
属性的设置器中放置处理已检查/未检查事件的逻辑:
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屋!