Automatically update CanExecute status when CanExecute is bound to an property
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
It troubling me for a long time too. Now I have a crazy idea, why not use the source generator to find all readed observable property in the CanExecute? Then generate NotifyCanExecuteChanged for those property.