Edit

Share via


NotifyCanExecuteChangedForAttribute Class

Definition

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 on a property 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))]
    public partial string Name { get; set; }

    public IRelayCommand GreetUserCommand { get; }
}

And with this, code analogous to this will be generated:
partial class MyViewModel
{
    public partial string Name
    {
        get => field;
        set
        {
            if (SetProperty(ref field, value))
            {
                GreetUserCommand.NotifyCanExecuteChanged();
            }
        }
    }
}
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)]
public sealed class NotifyCanExecuteChangedForAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)>]
type NotifyCanExecuteChangedForAttribute = class
    inherit Attribute
Public NotInheritable Class NotifyCanExecuteChangedForAttribute
Inherits Attribute
Inheritance
NotifyCanExecuteChangedForAttribute
Attributes

Remarks

Just like ObservablePropertyAttribute, this attribute can also be used on fields as well.

Constructors

NotifyCanExecuteChangedForAttribute(String, String[])

Initializes a new instance of the NotifyCanExecuteChangedForAttribute class.

NotifyCanExecuteChangedForAttribute(String)

Initializes a new instance of the NotifyCanExecuteChangedForAttribute class.

Properties

CommandNames

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

Applies to