Skip to content

CascadingValueSource<T> should automatically invoke NotifyChangedAsync for INotifyPropertyChanged values #53257

Closed
@kzu

Description

@kzu

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

The documentation on cascading values only mentions in passing that:

... all recipients are subscribed for update notifications, which are issued by calling NotifyChangedAsync

But there's no guidance on who should invoke that method and how to achieve that specially given two-way binding with such state.

This is contrary to the expectation in all other C# UI frameworks that typically leverage INotifyPropertyChanged to automatically notify any subscribers when changes are raised via the PropertyChanged event.

Describe the solution you'd like

Perhaps the constructor for CascadingValueSource<T> should detect whether the received value implements INotifyPropertyChanged and automatically subscribe to changes in that case, such as:

        if (value is INotifyPropertyChanged changed)
            changed.PropertyChanged += (sender, args) => this.NotifyChangedAsync();

Additional context

I have worked around this by creating my own factory for such value sources:

public static class CascadingValueSource
{
    public static CascadingValueSource<T> Create<T>(T value, bool isFixed)
    {
        var source = new CascadingValueSource<T>(value, isFixed);

        if (value is INotifyPropertyChanged changed)
            changed.PropertyChanged += (sender, args) => source.NotifyChangedAsync();

        return source;
    }
}

I find such factories for generics instances much more intuitive to use, turning the following:

new CascadingValueSource<NavMenuState>(new NavMenuState(), false)

to the slightly more concise:

CascadingValueSource.Create(new NavMenuState(), false)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions