CommunityToolkit / CommunityToolkit/dotnet

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

Open
#1,114 1 comment 0 reactions 0 assignees View on GitHub
bug :bug:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### 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:
```csharp
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
- [x] 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.