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

CallerMemberName does not work for indexer #177

Open
JohanLarsson opened this issue Nov 10, 2020 · 0 comments
Open

CallerMemberName does not work for indexer #177

JohanLarsson opened this issue Nov 10, 2020 · 0 comments

Comments

@JohanLarsson
Copy link
Collaborator

JohanLarsson commented Nov 10, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant