Browse Source

Merge pull request #11791 from timunie/fix/SubMenuCommandInvalidation

Fix: MenuItem invalidate CanExecute on opening a Menu
pull/11830/head
Tim 3 years ago
committed by GitHub
parent
commit
2787b886ce
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/Avalonia.Controls/MenuItem.cs
  2. 12
      tests/Avalonia.Controls.UnitTests/MenuItemTests.cs

8
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

12
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);
}
}

Loading…
Cancel
Save