Skip to content

CallerMemberName does not work for indexer #177

@JohanLarsson

Description

@JohanLarsson

Before:

public class ViewModel : INotifyPropertyChanged
{
    private readonly Dictionary<string, string> map = new Dictionary<string, string>{ { "1", "one" } };

    public event PropertyChangedEventHandler PropertyChanged;

    public string this[string index]
    {
        get => this.map[index];
        set
        {
            this.map[index] = value;
            this.OnPropertyChanged();
        }
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

After:

public class ViewModel : INotifyPropertyChanged
{
    private readonly Dictionary<string, string> map = new Dictionary<string, string>{ { "1", "one" } };

    public event PropertyChangedEventHandler PropertyChanged;

    public string this[string index]
    {
        get => this.map[index];
        set
        {
            this.map[index] = value;
            this.OnPropertyChanged("Item[]");
        }
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

When using [CallerMemberName] Itemis passed as name and it does not work with bindings.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions