Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 of 4 tasks
JoshStrauss opened this issue May 2, 2024 · 4 comments
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior

Comments

@JoshStrauss
Copy link

JoshStrauss commented May 2, 2024

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
  • 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 JoshStrauss added the bug 🐛 An unexpected issue that highlights incorrect behavior label May 2, 2024
@mohummedibrahim
Copy link

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();
  }

@JoshStrauss
Copy link
Author

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

@AndrewKeepCoding
Copy link

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

@suugbut
Copy link

suugbut commented Sep 6, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior
Projects
None yet
Development

No branches or pull requests

4 participants