Skip to content

Commit 6fd3327

Browse files
author
msftbot[bot]
authored
Fixed [AlsoNotifyChangeFor] attribute definition (#4242)
## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more options below that apply to this PR. --> - Bugfix <!-- - Feature --> <!-- - Code style update (formatting) --> <!-- - Refactoring (no functional changes, no api changes) --> <!-- - Build or CI related changes --> <!-- - Documentation content changes --> <!-- - Sample app changes --> <!-- - Other... Please describe: --> ## What is the current behavior? The `[AlsoNotifyChangeFor]` attribute can't be used multiple types, and a constructor is missing `params`. <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> ## What is the new behavior? Fixed the issues mentioned above. <!-- Describe how was this issue resolved or changed? --> ## PR Checklist Please check if your PR fulfills the following requirements: <!-- and remove the ones that are not applicable to the current PR --> - [X] Tested code with current [supported SDKs](../#supported) - [X] New component - [X] Pull Request has been submitted to the documentation repository [instructions](../blob/main/Contributing.md#docs). Link: <!-- docs PR link --> - [X] Added description of major feature to project description for NuGet package (4000 total character limit, so don't push entire description over that) - [X] If control, added to Visual Studio Design project - [X] Sample in sample app has been added / updated (for bug fixes / features) - [X] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/CommunityToolkit/WindowsCommunityToolkit-design-assets) - [X] New major technical changes in the toolkit have or will be added to the [Wiki](https://github.com/CommunityToolkit/WindowsCommunityToolkit/wiki) e.g. build changes, source generators, testing infrastructure, sample creation changes, etc... - [X] Tests for the changes have been added (for bug fixes / features) (if applicable) - [X] Header has been added to all new source files (run _build/UpdateHeaders.bat_) - [X] Contains **NO** breaking changes
2 parents 9d9800d + 9f887e6 commit 6fd3327

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

Microsoft.Toolkit.Mvvm/ComponentModel/Attributes/AlsoNotifyChangeForAttribute.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

@@ -67,7 +67,7 @@ namespace Microsoft.Toolkit.Mvvm.ComponentModel
6767
/// }
6868
/// </code>
6969
/// </summary>
70-
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
70+
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true, Inherited = false)]
7171
public sealed class AlsoNotifyChangeForAttribute : Attribute
7272
{
7373
/// <summary>
@@ -87,7 +87,7 @@ public AlsoNotifyChangeForAttribute(string propertyName)
8787
/// The other property names to also notify when the annotated property changes. This parameter can optionally
8888
/// be used to indicate a series of dependent properties from the same attribute, to keep the code more compact.
8989
/// </param>
90-
public AlsoNotifyChangeForAttribute(string propertyName, string[] otherPropertyNames)
90+
public AlsoNotifyChangeForAttribute(string propertyName, params string[] otherPropertyNames)
9191
{
9292
PropertyNames = new[] { propertyName }.Concat(otherPropertyNames).ToArray();
9393
}

UnitTests/UnitTests.NetCore/Mvvm/Test_ObservablePropertyAttribute.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void Test_AlsoNotifyChangeForAttribute_Events()
7171
model.Name = "Bob";
7272
model.Surname = "Ross";
7373

74-
CollectionAssert.AreEqual(new[] { nameof(model.Name), nameof(model.FullName), nameof(model.Surname), nameof(model.FullName) }, propertyNames);
74+
CollectionAssert.AreEqual(new[] { nameof(model.Name), nameof(model.FullName), nameof(model.Alias), nameof(model.Surname), nameof(model.FullName), nameof(model.Alias) }, propertyNames);
7575
}
7676

7777
[TestCategory("Mvvm")]
@@ -169,13 +169,16 @@ public sealed partial class DependentPropertyModel
169169
{
170170
[ObservableProperty]
171171
[AlsoNotifyChangeFor(nameof(FullName))]
172+
[AlsoNotifyChangeFor(nameof(Alias))]
172173
private string? name;
173174

174175
[ObservableProperty]
175-
[AlsoNotifyChangeFor(nameof(FullName))]
176+
[AlsoNotifyChangeFor(nameof(FullName), nameof(Alias))]
176177
private string? surname;
177178

178179
public string FullName => $"{Name} {Surname}";
180+
181+
public string Alias => $"{Name?.ToLower()}{Surname?.ToLower()}";
179182
}
180183

181184
public partial class MyFormViewModel : ObservableValidator

0 commit comments

Comments
 (0)