0

I have the following line in a .XAML file:

<CheckBox x:Name="checker"  Grid.Column="1" Grid.Row="6" Command="{Binding LoadCommand}"></CheckBox>

Now my question is - what is the best way to have different Commands for the checkbox "checked" and checkbox "unchecked".

In my ViewModel I have two different functions (LoadCommand and LoadActiveCommand), one should be loaded when the checkbox is checked and the other when the checkbox is unchecked.

What is the best way to get the result I wish? Is there a way to do it directly in the .XAML file or do I have to do this in xaml.cs?

shomsky
  • 43
  • 8
  • 1
    Possible duplicate of [Executing a command on Checkbox.Checked or Unchecked](https://stackoverflow.com/questions/5566050/executing-a-command-on-checkbox-checked-or-unchecked) – Sinatr Dec 22 '17 at 10:40
  • Look at [this answer](https://stackoverflow.com/a/15249090/1997232). – Sinatr Dec 22 '17 at 10:41
  • Maybe simplest is to bind `IsChecked` (or pass it as `CommandParameter`) to some property, which value you can check in command event handler. – Sinatr Dec 22 '17 at 10:44
  • Firstly you are not assigning the `IsChecked` property in your xaml so it will always be false. Secondly if you do then you already have a property that you can use in your `ViewModel` this means that the command should rely on the property that you use to bind to `IsChecked` property. – XAMlMAX Dec 22 '17 at 11:25

1 Answers1

0

Instead of binding to a command you could bind to a setter and perform the action depending on the value, something like:

public bool LoadCommand
{
    set
    {
        if(value == true)
        {
            // do something;
        }
        else
        {
            //do something else;
        }
}
stuicidle
  • 297
  • 1
  • 8