CommunityToolkit / CommunityToolkit/dotnet

Add support for combining NotifyParentPropertyAttribute and ObservablePropertyAttribute

Open
#602 0 comments 0 reactions 0 assignees View on GitHub
feature request :mailbox_with_mail:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Overview

`System.ComponentModel.NotifyParentPropertyAttribute` can only decorate a property but a `ObservableProperty` must be defined as a field so that a property can be generated automatically.

To allow them to work together, we should either provide a CommunityToolkit implementation of `NotifyParentPropertyAttribute` or allow `ObservablePropertyAttribute` to be used with a partial property (https://github.com/CommunityToolkit/dotnet/issues/555).

For now, the latter is infeasible as C# does not allow partial properties.

### API breakdown

```csharp
namespace CommunityToolkit.Mvvm.ComponentModel;

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
public sealed class NotifyParentPropertyAttribute : Attribute
{
public NotifyParentPropertyAttribute(bool notifyParent)
{
NotifyParent = notifyParent;
}

public bool NotifyParent { get; }
}
```

### Usage example

```csharp
public partial class ChildModel : ObservableObject
{
[ObservableProperty]
[NotifyParentChanged(true)]
private string _name;
}

public partial class ParentModel : ObservableObject
{
[ObservableProperty]
private ChildModel _child = new ();
}
```

When the `Child.Name` property gets updated, the `ParentModel` should be notified for the change of the `Child` property, i.e., `OnPropertyChanged(nameof(Child))` should be called.
```csharp
var parentModel = new ParentModel();
parentModel.Child.Name = "something"; // should call OnPropertyChanged(nameof(Child))
```

### Breaking change?

No

### Alternatives

Currently one has to choose between `[NotifyParentChanged(true)]` and `[ObservableProperty]`.

### Additional context

_No response_

### Help us help you

Yes, but only if others can assist

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.