CommunityToolkit / CommunityToolkit/dotnet

Automatically update CanExecute status when CanExecute is bound to an property

Open
#959 1 comment 3 reactions 0 assignees View on GitHub
feature request :mailbox_with_mail:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### 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

```C#
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.

```C#
[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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.