1

I am using a Button to change my IsSelected property. I am using MVVM Light's ViewModelBase to raise PropertyChanged event.

Model

private bool _isSelected = true;

public bool IsSelected
{
    get
    {
        return _isSelected;
    }
    set
    {
        Set(IsSelected, ref _isSelected, value);
        Messenger.Default.Send(Message.message);
    }
}

//ICommand
public const string isSelectedCommandPropertyName = "isSelectedCommand";

private ICommand _isSelectedCommand;

public ICommand isSelectedCommand
{
    get
    {
        IsSelected = !IsSelected;
        return null;
    }
    set
    {
        Set(isSelectedCommandPropertyName, ref _isSelectedCommand, value);
        Messenger.Default.Send(Message.message);
    }
}

View

<Button Command="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> Click </Button>

This set of codes works successfully if I use ToggleButton's Ischecked property. This code is working EXCEPT for the button. I think there's something I missed.

LindaSingh
  • 84
  • 1
  • 13
  • Do you get an exception? Compiler error? How would the command know what to set the property to? – Fildor Sep 30 '19 at 07:46
  • 1
    `ToggleButton.IsChecked` is a boolean property, so binding a boolean VM property works there... `Button.Commind` is an `ICommand` property, so you can't expect a boolean to work there. Question: since you already discovered `ToggleButton`, why not use it? – grek40 Sep 30 '19 at 07:49
  • First of all: Do you get any error messages? Be it runtime errors or compilation errors? Then: A button command has no boolean argument. You need additional code, if you want to toggle an each click. ToggleButton does that for you by having a boolean property to bind your viewmodel to. In other words: You are trying to fit a square block into a round hole. – Fildor Sep 30 '19 at 07:53
  • @Fildor, I already answered you, I didn't get any errors, be it runtime or compilation. Its just that, it's plainly not working. The button ripples on click but nothing. – LindaSingh Sep 30 '19 at 07:55
  • Oh - "get" as in "get" not in "understand" ... ;D sorry, my bad. – Fildor Sep 30 '19 at 08:03
  • @Fildor, I added `ICommand` but it automatically changes the `_isSelected` property. See edit for the code – LindaSingh Sep 30 '19 at 09:15
  • Ah, that's not how you do it. I had a tutorial on MVVM Light's Command usage somewhere ... just a second. – Fildor Sep 30 '19 at 09:19
  • 1
    Have a look at this: https://stackoverflow.com/a/6754757/982149 – Fildor Sep 30 '19 at 09:21

1 Answers1

1

Your ICommand implementation is wrong, it is also stated by @Fildor in the comments who linked this question that helped me to come up with this answer.

In the Model, you need the RelayCommand to be binded with the View's Button.

private RelayCommand IsSelectedCommand {get; set;}

// then your void isSelected function, this is the command to be called if button is clicked
public void isSelectedCommand()
{
    IsSelected = !IsSelected;
}

public your_model()
{
    this.IsSelectedCommand = new RelayCommand(this.isSelectedCommand)
}

Then bind this RelayCommand's IsSelectedCommand instead of binding directly your IsSelected in your Button in your View.

<Button Command="{Binding IsSelectedCommand, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> Click </Button>
CodeRed
  • 905
  • 1
  • 6
  • 24