Skip to content

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

@Earlh21

Description

@Earlh21

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request 📬A request for new changes to improve functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions