From 8aaaeb95d21d4719f02f0fead8540572b6542d2d Mon Sep 17 00:00:00 2001 From: yoyo <143886016+zrt2399@users.noreply.github.com> Date: Thu, 4 Jun 2026 05:51:36 +0800 Subject: [PATCH] Fix NotificationCard pseudo class state when NotificationType changes (#21490) * Fix NotificationCard pseudo class state when NotificationType changes * Add test for NotificationCard pseudo-class updates --- .../Notifications/NotificationCard.cs | 22 +++---------- .../NotificationsTests.cs | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/Avalonia.Controls/Notifications/NotificationCard.cs b/src/Avalonia.Controls/Notifications/NotificationCard.cs index 69f568f9f6..5805adb28e 100644 --- a/src/Avalonia.Controls/Notifications/NotificationCard.cs +++ b/src/Avalonia.Controls/Notifications/NotificationCard.cs @@ -170,24 +170,10 @@ namespace Avalonia.Controls.Notifications private void UpdateNotificationType() { - switch (NotificationType) - { - case NotificationType.Error: - PseudoClasses.Add(":error"); - break; - - case NotificationType.Information: - PseudoClasses.Add(":information"); - break; - - case NotificationType.Success: - PseudoClasses.Add(":success"); - break; - - case NotificationType.Warning: - PseudoClasses.Add(":warning"); - break; - } + PseudoClasses.Set(":error", NotificationType == NotificationType.Error); + PseudoClasses.Set(":information", NotificationType == NotificationType.Information); + PseudoClasses.Set(":success", NotificationType == NotificationType.Success); + PseudoClasses.Set(":warning", NotificationType == NotificationType.Warning); } } } diff --git a/tests/Avalonia.Controls.UnitTests/NotificationsTests.cs b/tests/Avalonia.Controls.UnitTests/NotificationsTests.cs index e8411f7da1..22e8edad82 100644 --- a/tests/Avalonia.Controls.UnitTests/NotificationsTests.cs +++ b/tests/Avalonia.Controls.UnitTests/NotificationsTests.cs @@ -113,4 +113,35 @@ namespace Avalonia.Controls.UnitTests Assert.True(!((WindowNotificationManager)manager).Notifications.Any(x => !x.IsClosing)); } } + + public class NotificationCardTests : ScopedTestBase + { + [Fact] + public void Should_Update_Pseudoclasses_When_NotificationType_Changes() + { + var target = new NotificationCard(); + + target.NotificationType = NotificationType.Error; + AssertPseudoClasses(target, ":error"); + + target.NotificationType = NotificationType.Information; + AssertPseudoClasses(target, ":information"); + + target.NotificationType = NotificationType.Success; + AssertPseudoClasses(target, ":success"); + + target.NotificationType = NotificationType.Warning; + AssertPseudoClasses(target, ":warning"); + + static void AssertPseudoClasses(NotificationCard target, string expected) + { + Assert.Contains(expected, target.Classes); + + foreach (var pseudoclass in new[] { ":error", ":information", ":success", ":warning" }.Where(x => x != expected)) + { + Assert.DoesNotContain(pseudoclass, target.Classes); + } + } + } + } }