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

Add support for combining NotifyParentPropertyAttribute and ObservablePropertyAttribute #602

Open
stcmz opened this issue Feb 10, 2023 · 0 comments
Labels
feature request 📬 A request for new changes to improve functionality

Comments

@stcmz
Copy link

stcmz commented Feb 10, 2023

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 (#555).

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

API breakdown

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

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.

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

@stcmz stcmz added the feature request 📬 A request for new changes to improve functionality label Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request 📬 A request for new changes to improve functionality
Projects
None yet
Development

No branches or pull requests

1 participant