Bladeren bron

Merge pull request #11791 from timunie/fix/SubMenuCommandInvalidation

Fix: MenuItem invalidate CanExecute on opening a Menu
Tim 2 jaren geleden
bovenliggende
commit
2787b886ce
2 gewijzigde bestanden met toevoegingen van 14 en 6 verwijderingen
  1. 8 0
      src/Avalonia.Controls/MenuItem.cs
  2. 6 6
      tests/Avalonia.Controls.UnitTests/MenuItemTests.cs

+ 8 - 0
src/Avalonia.Controls/MenuItem.cs

@@ -394,6 +394,14 @@ namespace Avalonia.Controls
             _isEmbeddedInMenu = parent?.FindLogicalAncestorOfType<IMenu>(true) != null;
         }
 
+        /// <inheritdoc />
+        protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
+        {
+            base.OnAttachedToVisualTree(e);
+            
+            TryUpdateCanExecute();
+        }
+
         protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
         {
             // This will cause the hotkey manager to dispose the observer and the reference to this control

+ 6 - 6
tests/Avalonia.Controls.UnitTests/MenuItemTests.cs

@@ -209,13 +209,13 @@ namespace Avalonia.Controls.UnitTests
                 Assert.Equal(0, canExecuteCallCount);
                 
                 contextMenu.Open();
-                Assert.Equal(2, canExecuteCallCount);//2 because popup is changing logical child
+                Assert.Equal(3, canExecuteCallCount);// 3 because popup is changing logical child and moreover we need to invalidate again after the item is attached to the visual tree
 
                 command.RaiseCanExecuteChanged();
-                Assert.Equal(3, canExecuteCallCount);
+                Assert.Equal(4, canExecuteCallCount);
 
                 target.CommandParameter = true;
-                Assert.Equal(4, canExecuteCallCount);
+                Assert.Equal(5, canExecuteCallCount);
             }
         }
         
@@ -249,13 +249,13 @@ namespace Avalonia.Controls.UnitTests
                 Assert.Equal(0, canExecuteCallCount);
 
                 flyout.ShowAt(button);
-                Assert.Equal(1, canExecuteCallCount);
+                Assert.Equal(2, canExecuteCallCount); // 2 because we need to invalidate after the item is attached to the visual tree
 
                 command.RaiseCanExecuteChanged();
-                Assert.Equal(2, canExecuteCallCount);
+                Assert.Equal(3, canExecuteCallCount);
 
                 target.CommandParameter = true;
-                Assert.Equal(3, canExecuteCallCount);
+                Assert.Equal(4, canExecuteCallCount);
             }
         }