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

Allow ability to raise CanExecuteChanged event of all commands for any property change #1041

Open
bwood4 opened this issue Jan 9, 2025 · 0 comments
Labels
feature request 📬 A request for new changes to improve functionality

Comments

@bwood4
Copy link

bwood4 commented Jan 9, 2025

Overview

I've found that I frequently have long lists of commands to update when properties change. It might be nice to be able to flag a class to raise CanExecuteChanged events for all commands whenever any property changes, then you wouldn't have to worry about remembering each command for each dependent property.

I think it would cut down on a decent amount of boilerplate with a fairly minor performance hit from unnecessarily calling CanExecute.

API breakdown

I think the API would just be an attribute you can add to an ObservableObject like this:

[AlwaysNotifyCanExecute]
public partial class ViewModel : ObservableObject
{
...
}

Usage example

[AlwaysNotifyCanExecute]
public partial class ViewModel : ObservableObject
{
    public IRelayCommand DoThing1Command { get; }
    
    [RelayCommand]
    public void DoThing2() { }
}

would generate the following code:

partial class ViewModel
{
    protected overrides void OnPropertyChanged(PropertyChangedEventArgs args)
    {
        base.OnPropertyChanged(args);
        DoThing1Command.NotifyCanExecuteChanged();
        DoThing2Command.NotifyCanExecuteChanged();
    }
}

Breaking change?

No

Alternatives

Edit:

You could also require/allow the attribute to take in the command names that you want to always update, ie:

[AlwaysNotifyCanExecuteChanged(nameOf(DoThing1Command))]
public partial class ViewModel : ObservableObject

or maybe place the attribute on each command or tie it in to the RelayCommandAttribute:

public partial class ViewModel : ObservableObject
{
    [AlwaysNotifyCanExecute]
    public IRelayCommand DoThing1Command { get; }
    
    [RelayCommand(AlwaysNotifyCanExecute = true)]
    public void DoThing2() { }
}

Additional context

Since this is an opt-in feature, there shouldn't be breaking changes and it makes it clear at the top of the class what is happening behind the scenes.

Help us help you

No, just wanted to propose this

@bwood4 bwood4 added the feature request 📬 A request for new changes to improve functionality label Jan 9, 2025
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