dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

Setting 'CanExecute' doesn't change the status of Button controls when they are bound to RelayCommand

Open JoshStrauss opened this issue 1 year ago • 4 comments

Setting 'CanExecute' doesn't change the status of Button controls when they are bound to RelayCommand. I recently upgraded from GalaSoft.MvvmLight.Command.RelayCommand where this was possible. However, using the RelayCommand in the tool kit doesn't enable and disable buttons consistently in the UI.

I've manually copied this class back into the solution and it fixes the problem.

https://github.com/lbugnion/mvvmlight/blob/aa657f7150730ea9d82d1077ffa0038affc400ca/V3/GalaSoft.MvvmLight/GalaSoft.MvvmLight%20(NET35)/Command/RelayCommand.cs#L4

Regression

No response

Steps to reproduce

xaml
 <Button Content="Click me" Command="{Binding MyCommand}" />
 <Button Content="Disable Command" Command="{Binding DisableCommand}" />

c#

  public RelayCommand MyCommand => new RelayCommand(ExecuteMyCommand, CanExecuteMyCommand);
  public RelayCommand DisableCommand => new RelayCommand(ExecuteDisableCommand);

  [ObservableProperty]
  private bool _isEnabled = true;

  private void ExecuteMyCommand()
  {
      // Command execution logic
  }

  private bool CanExecuteMyCommand()
  {
      return IsEnabled;
  }

  private void ExecuteDisableCommand()
  {
      IsEnabled = false;
  }

Expected behavior

If can canExecute is false button to become disabled

Screenshots

No response

IDE and version

VS 2022

IDE version

No response

Nuget packages

  • [ ] CommunityToolkit.Common
  • [ ] CommunityToolkit.Diagnostics
  • [ ] CommunityToolkit.HighPerformance
  • [X] CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

8.2.2

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item

JoshStrauss avatar May 02 '24 15:05 JoshStrauss

I think you'll need to call the IRelayCommand.NotifyCanExecuteChanged() for the CanExecute to observe the status changes. i.e. something like this.

  private void ExecuteDisableCommand()
  {
      IsEnabled = false;
      MyCommand.NotifyCanExecuteChanged();
  }

mohummedibrahim avatar May 11 '24 20:05 mohummedibrahim

That has no effect, the breakpoint on CanExecuteMyCommand() is never hit until the user clicks on the button. So the buttons always appear enabled even though IsEnabled is false.

However, doing this does fix it =>

[ObservableProperty] [NotifyCanExecuteChangedFor(nameof(ExecuteMyCommandCommand))] private bool _isEnabled = true;

[RelayCommand(CanExecute = nameof(CanExecuteMyCommand))] private void ExecuteMyCommand() { // Command execution logic }

so there must be something else missing here

JoshStrauss avatar May 17 '24 12:05 JoshStrauss

Using NotifyCanExecuteChangedFor should fix your issue. What is still missing?🤔

AndrewKeepCoding avatar Jun 10 '24 01:06 AndrewKeepCoding

@JoshStrauss As you have fixed the problem, you can close it.

suugbut avatar Sep 06 '24 06:09 suugbut