3

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

4 Answers 4

9

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
       }
   }
2
  • Hi Orchestraor, i like your answer, kindly clear my on couple of things.You said, i need to bind my property to a Dependency property, so first i have to create a dependency property in my VM? and do i need to set the Content="Refresh" property or it could do without refreshing it?
    – Manvinder
    Commented Feb 15, 2012 at 12:48
  • Hi Manav, Yes you would have to create a property just like I specified in the code. I call it a dependency property because it raises the NotifyPropertChanged event. that's why your ViewModel must implement the INotifyPropertChanged interface. You don't need to set any other property besides the "IsChecked" property, I only added them because they existed in Jon's question.
    – Dror
    Commented Feb 15, 2012 at 12:57
3

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.

3
  • what is the cmd namespace, isn't it from the MVVM Light toolkit? Commented Feb 15, 2012 at 12:33
  • Thanks for the clarification. The thing is, not everybody is using MVVM Light (I prefer Prism) so calling it "the only preferred way" is a bit strong :-) Commented Feb 15, 2012 at 12:36
  • Some examples of how to do this without MVVM Light can be found here.
    – Scott
    Commented Aug 1, 2014 at 15:37
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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.