如何将命令绑定到复选框

发布于 2025-01-05 23:57:59 字数 320 浏览 2 评论 0原文

我正在尝试将复选框 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

前事休说 2025-01-12 23:57:59

您不需要将其绑定到事件。
您需要做的就是将 IsChecked 绑定到布尔依赖属性,并在其设置器上执行您想要的任何逻辑。

像这样 -

<CheckBox x:Name="radRefresh" IsChecked="{Binding IsChecked, Mode=TwoWay}" Content=" Refresh "  Margin="10,25,0,0"  />

这个 xaml 应该将您绑定到 VM 上的这个属性

  public bool IsChecked
  {
      get
      {
          return isChecked;
      }
      set
      {
          isChecked = value;
          NotifyPropertChanged("IsChecked");

          //Add any logic you'd like here
       }
   }

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 -

<CheckBox x:Name="radRefresh" IsChecked="{Binding IsChecked, Mode=TwoWay}" Content=" Refresh "  Margin="10,25,0,0"  />

This xaml should bind you to this property on the VM

  public bool IsChecked
  {
      get
      {
          return isChecked;
      }
      set
      {
          isChecked = value;
          NotifyPropertChanged("IsChecked");

          //Add any logic you'd like here
       }
   }
颜漓半夏 2025-01-12 23:57:59

这会起作用...

<代码>


;

其中 i 是

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

为 cmd 添加命名空间

    xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"

如果您正在使用 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

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

Add namespace for cmd

    xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"

This is one of the preferred ways for MVVM if you are utilizing the MVVMLight Framework.

酒中人 2025-01-12 23:57:59

这是一个简单的情况:

  1. 将 CheckBox.IsChecked 绑定到 ViewModel 中的布尔值。
  2. 在您的视图模型中,订阅属性更改事件并观察要更改的布尔值。

完毕。

This is a simple situation:

  1. Bind the CheckBox.IsChecked to a boolean value in your ViewModel.
  2. In your viewmodel, subscribe to the property changed event and watch for the boolean value to change.

Done.

寄意 2025-01-12 23:57:59

这是因为只有少数控件(我头顶的 ButtonMenuItem)实现了直接绑定命令的功能。

如果您需要绑定到控件,我建议创建一个 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 property IsChecked is changed in codebehind.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文