Table of Contents

Class NotifyCanExecuteChangedForAttribute

Namespace
CommunityToolkit.Mvvm.ComponentModel
Assembly
CommunityToolkit.Mvvm.dll

An attribute that can be used to support IRelayCommand properties in generated properties. When this attribute is used, the generated property setter will also call NotifyCanExecuteChanged() for the properties specified in the attribute data, causing the validation logic for the command to be executed again. This can be useful to keep the code compact when there are one or more dependent commands that should also be notified when a property is updated. If this attribute is used in a field without ObservablePropertyAttribute, it is ignored (just like NotifyPropertyChangedForAttribute).

In order to use this attribute, the target property has to implement the IRelayCommand interface.

This attribute can be used as follows:

partial class MyViewModel : ObservableObject
{
    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(GreetUserCommand))]
    private string name;
public IRelayCommand GreetUserCommand { get; }

}

And with this, code analogous to this will be generated:
partial class MyViewModel
{
    public string Name
    {
        get => name;
        set
        {
            if (SetProperty(ref name, value))
            {
                GreetUserCommand.NotifyCanExecuteChanged();
            }
        }
    }
}
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true, Inherited = false)]
public sealed class NotifyCanExecuteChangedForAttribute : Attribute
Inheritance
NotifyCanExecuteChangedForAttribute
Inherited Members

Constructors

NotifyCanExecuteChangedForAttribute(string)

Initializes a new instance of the NotifyCanExecuteChangedForAttribute class.

public NotifyCanExecuteChangedForAttribute(string commandName)

Parameters

commandName string

The name of the command to also notify when the annotated property changes.

NotifyCanExecuteChangedForAttribute(string, params string[])

Initializes a new instance of the NotifyCanExecuteChangedForAttribute class.

public NotifyCanExecuteChangedForAttribute(string commandName, params string[] otherCommandNames)

Parameters

commandName string

The name of the property to also notify when the annotated property changes.

otherCommandNames string[]

The other command names to also notify when the annotated property changes. This parameter can optionally be used to indicate a series of dependent commands from the same attribute, to keep the code more compact.

Properties

CommandNames

Gets the command names to also notify when the annotated property changes.

public string[] CommandNames { get; }

Property Value

string[]