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

Automatically update CanExecute status when CanExecute is bound to an property #959

Open
Earlh21 opened this issue Oct 15, 2024 · 0 comments
Labels
feature request 📬 A request for new changes to improve functionality

Comments

@Earlh21
Copy link

Earlh21 commented Oct 15, 2024

Overview

It's not feasible to automatically update the CanExecute status for a command when it's bound to a method.

But when bound to a bool property, CanExecute should automatically update without having to include [NotifyCanExecuteChangedFor] on the property.

API breakdown

No API changes. This is a change in behavior of the existing API.

Usage example

public partial class SampleViewModel : ObservableObject
{
    [ObservableProperty] private bool boolProperty;
    
    [RelayCommand(CanExecute = nameof(BoolProperty))]
    private void SampleCommand()
    {
        // Command logic
    }
}

In this example, it seems logically redundant to declare the NotifyCanExecute attribute - the purpose of NotifyPropertyChanged is to notify anything that depends directly on the property.

And if there are a lot of commands that depend solely on that property (e.g. disable all other commands while editing a single part of the view), it's unwieldy to add the attribute for every command.

Breaking change?

I'm not sure

Alternatives

Here's the code that prompted me to suggest this change.

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsNotEditingDetails))]
[NotifyCanExecuteChangedFor(nameof(EditDetailsCommand))]
[NotifyCanExecuteChangedFor(nameof(SaveDetailsCommand))]
[NotifyCanExecuteChangedFor(nameof(CancelEditDetailsCommand))]
[NotifyCanExecuteChangedFor(nameof(NewPanelCommand))]
[NotifyCanExecuteChangedFor(nameof(AddCommonPanelsCommand))]
[NotifyCanExecuteChangedFor(nameof(EditPanelCommand))]
[NotifyCanExecuteChangedFor(nameof(RemovePanelCommand))]
[NotifyCanExecuteChangedFor(nameof(AddPanelInstanceCommand))]
[NotifyCanExecuteChangedFor(nameof(RemovePanelInstanceCommand))]
[NotifyCanExecuteChangedFor(nameof(CreateCommonPanelCommand))]
private bool isEditingDetails;

Additional context

No response

Help us help you

No, just wanted to propose this

@Earlh21 Earlh21 added the feature request 📬 A request for new changes to improve functionality label Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request 📬 A request for new changes to improve functionality
Projects
None yet
Development

No branches or pull requests

1 participant