admin管理员组

文章数量:1295723

In a console project.

Program.cs

using ObservablePropertyTests;

NotifyOtherProperty notifyOtherProperty = new NotifyOtherProperty
{
    Name = "Foo",
    AnotherName = "2Foo",
};
notifyOtherProperty.Name = "Bar";

ObservablePropertyTest.cs

using CommunityToolkit.Mvvm.ComponentModel;

namespace ObservablePropertyTests
{
    public partial class NotifyOtherProperty : ObservableObject
    {
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(AnotherName))]
        private string? name;

        [ObservableProperty]
        private string? anotherName;

        partial void OnNameChanging(string? oldValue, string? newValue)
        {
            Console.WriteLine("OnNameChanging");
            Console.WriteLine($"{oldValue} -> {newValue}");
        }

        partial void OnAnotherNameChanged(string? oldValue, string? newValue)
        {
            Console.WriteLine("OnAnotherNameChanged");
            Console.WriteLine($"{oldValue} -> {newValue}");
        }
    }
}

Output:

OnNameChanging
 -> Foo
OnAnotherNameChanged
 -> 2Foo
OnNameChanging
Foo -> Bar

Why isn't OnAnotherNameChanged called by the second assignment("Foo" to "Bar")?

.Net version: 8.0.404

Mvvm Toolkit version: 8.4.0

本文标签: Why NotifyPropertyChangedFor not working for OnAnotherNameChanged in C MVVM toolkitStack Overflow