如何将命令绑定到复选框
我正在尝试将复选框 checkchange 事件绑定到命令 - MVVM 为什么这不起作用?或者在按钮上执行相同操作时执行任何操作?
<CheckBox x:Name="radRefresh" IsChecked="{BindingREADY, Mode=TwoWay}" Command="{Binding Refresh_Command}" Content=" Refresh " Margin="10,25,0,0" />
<Button Command="{Binding Refresh_Command}" />
谢谢
I'm trying to bind the checkbox checkchange event to a command - MVVM
Why doesn't this work? or do anything while the same works on button?
<CheckBox x:Name="radRefresh" IsChecked="{BindingREADY, Mode=TwoWay}" Command="{Binding Refresh_Command}" Content=" Refresh " Margin="10,25,0,0" />
<Button Command="{Binding Refresh_Command}" />
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不需要将其绑定到事件。
您需要做的就是将 IsChecked 绑定到布尔依赖属性,并在其设置器上执行您想要的任何逻辑。
像这样 -
这个 xaml 应该将您绑定到 VM 上的这个属性
You don't need to bind it to an event.
All you need to do is bind IsChecked to a boolean dependency property, and do whatever logic you'd like on its setter.
Like this -
This xaml should bind you to this property on the VM
这会起作用...
<代码>
;
其中 i 是
为 cmd 添加命名空间
如果您正在使用 MVVMLight 框架,这是 MVVM 的首选方法之一。
This will work...
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding Path=YourCommand,Mode=OneWay}" CommandParameter="{Binding IsChecked, ElementName=YourCheckBox}"></cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
where i is
Add namespace for cmd
This is one of the preferred ways for MVVM if you are utilizing the MVVMLight Framework.
这是一个简单的情况:
完毕。
This is a simple situation:
Done.
这是因为只有少数控件(我头顶的
Button
、MenuItem
)实现了直接绑定命令的功能。如果您需要绑定到控件,我建议创建一个
UserControl
,当 CheckBox 的属性IsChecked
在代码隐藏中更改时,它会执行 ViewModel 的命令。That would be because only a handful of controls (
Button
,MenuItem
of the top of my head) implement the functionality to directly bind commands.If you need to bind to a control, I would recommend creating a
UserControl
that executes the ViewModel's command when the CheckBox's propertyIsChecked
is changed in codebehind.