From 9f887e65f31270427796d792bd6a1709283c6c7d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 12 Sep 2021 20:26:40 +0200 Subject: [PATCH] Fixed [AlsoNotifyChangeFor] attribute definition --- .../Attributes/AlsoNotifyChangeForAttribute.cs | 6 +++--- .../Mvvm/Test_ObservablePropertyAttribute.cs | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Microsoft.Toolkit.Mvvm/ComponentModel/Attributes/AlsoNotifyChangeForAttribute.cs b/Microsoft.Toolkit.Mvvm/ComponentModel/Attributes/AlsoNotifyChangeForAttribute.cs index ae5a66feaf3..0eb967a4c32 100644 --- a/Microsoft.Toolkit.Mvvm/ComponentModel/Attributes/AlsoNotifyChangeForAttribute.cs +++ b/Microsoft.Toolkit.Mvvm/ComponentModel/Attributes/AlsoNotifyChangeForAttribute.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -67,7 +67,7 @@ namespace Microsoft.Toolkit.Mvvm.ComponentModel /// } /// /// - [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)] + [AttributeUsage(AttributeTargets.Field, AllowMultiple = true, Inherited = false)] public sealed class AlsoNotifyChangeForAttribute : Attribute { /// @@ -87,7 +87,7 @@ public AlsoNotifyChangeForAttribute(string propertyName) /// The other property names to also notify when the annotated property changes. This parameter can optionally /// be used to indicate a series of dependent properties from the same attribute, to keep the code more compact. /// - public AlsoNotifyChangeForAttribute(string propertyName, string[] otherPropertyNames) + public AlsoNotifyChangeForAttribute(string propertyName, params string[] otherPropertyNames) { PropertyNames = new[] { propertyName }.Concat(otherPropertyNames).ToArray(); } diff --git a/UnitTests/UnitTests.NetCore/Mvvm/Test_ObservablePropertyAttribute.cs b/UnitTests/UnitTests.NetCore/Mvvm/Test_ObservablePropertyAttribute.cs index f7c596ba438..280a0d82e48 100644 --- a/UnitTests/UnitTests.NetCore/Mvvm/Test_ObservablePropertyAttribute.cs +++ b/UnitTests/UnitTests.NetCore/Mvvm/Test_ObservablePropertyAttribute.cs @@ -71,7 +71,7 @@ public void Test_AlsoNotifyChangeForAttribute_Events() model.Name = "Bob"; model.Surname = "Ross"; - CollectionAssert.AreEqual(new[] { nameof(model.Name), nameof(model.FullName), nameof(model.Surname), nameof(model.FullName) }, propertyNames); + CollectionAssert.AreEqual(new[] { nameof(model.Name), nameof(model.FullName), nameof(model.Alias), nameof(model.Surname), nameof(model.FullName), nameof(model.Alias) }, propertyNames); } [TestCategory("Mvvm")] @@ -169,13 +169,16 @@ public sealed partial class DependentPropertyModel { [ObservableProperty] [AlsoNotifyChangeFor(nameof(FullName))] + [AlsoNotifyChangeFor(nameof(Alias))] private string? name; [ObservableProperty] - [AlsoNotifyChangeFor(nameof(FullName))] + [AlsoNotifyChangeFor(nameof(FullName), nameof(Alias))] private string? surname; public string FullName => $"{Name} {Surname}"; + + public string Alias => $"{Name?.ToLower()}{Surname?.ToLower()}"; } public partial class MyFormViewModel : ObservableValidator