Skip to content

Error MVVMTK0015 when the desired NotifyPropertyChangedFor property is an explicit interface implementation #1114

@jphorv-bdo

Description

@jphorv-bdo

Describe the bug

A compilation error results for a situation that we think should be valid. Specifically:

error MVVMTK0015: The target(s) of [NotifyPropertyChangedFor] must be a (different) accessible property, but "(the notify-for property name)" has no (other) matches in type (my type) (https://aka.ms/mvvmtoolkit/errors/mvvmtk0015)

It appears that the toolkit's conditions for NotifiyPropertyChangedFor are not satisfied when the property exists only as an explicitly-implemented interface property.

Regression

No response

Steps to reproduce

Given this simplified reproduction:

public interface IMenuItem
{
    bool Enabled { get; }
}

public partial class MenuItem : ObservableObject, IMenuItem
{
    [ObservableProperty]
    [NotifyPropertyChangedFor(nameof(IMenuItem.Enabled))]
    public partial int ValueThatAffectsEnabled { get; set; }

    bool IMenuItem.Enabled => ValueThatAffectsEnabled > 0;
}

Compiling results in the following error:

error MVVMTK0015: The target(s) of [NotifyPropertyChangedFor] must be a (different) accessible property, but "Enabled" has no (other) matches in type MenuItem (https://aka.ms/mvvmtoolkit/errors/mvvmtk0015)

Expected behavior

We expect this to be allowed by the NotifyPropertyChangedFor attribute's validation because:

  1. There is a public property named Enabled, it's simply of lessened visibility (only visible through the interface)
  2. It is a very common for our views to work with their View Models as interfaces, not concrete types. This lets us swap in/out compatible view model types.

We can work around this:

  • We could make those properties not explicit interface implementations (we'd rather not, we design member visibility very intentionally).
  • We could add OnXXXXChanged() partial methods to manually call OnPropertyChanged("the desired property").

but it would be better if the library would allow this.

Screenshots

No response

IDE and version

VS 2022

IDE version

17.14.10

Nuget packages

  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

8.4.0

Additional context

No response

Help us help you

No, just wanted to report this

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛An unexpected issue that highlights incorrect behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions