diff --git a/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs b/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs index 0badd918cc..73854b9f60 100644 --- a/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs +++ b/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs @@ -338,7 +338,7 @@ namespace Avalonia.Controls.Platform { if (item.IsSubMenuOpen) { - if (item.Parent is Menu) + if (item.IsTopLevel) { CloseMenu(item); } diff --git a/tests/Avalonia.Controls.UnitTests/Platform/DefaultMenuInteractionHandlerTests.cs b/tests/Avalonia.Controls.UnitTests/Platform/DefaultMenuInteractionHandlerTests.cs index 87b235dce7..df1846c617 100644 --- a/tests/Avalonia.Controls.UnitTests/Platform/DefaultMenuInteractionHandlerTests.cs +++ b/tests/Avalonia.Controls.UnitTests/Platform/DefaultMenuInteractionHandlerTests.cs @@ -1,6 +1,7 @@ using System; using Avalonia.Controls.Platform; using Avalonia.Input; +using Avalonia.Interactivity; using Moq; using Xunit; @@ -110,6 +111,22 @@ namespace Avalonia.Controls.UnitTests.Platform Assert.True(e.Handled); } + [Fact] + public void Click_On_Open_TopLevel_Menu_Closes_Menu() + { + var target = new DefaultMenuInteractionHandler(false); + var menu = Mock.Of(); + var item = Mock.Of(x => + x.IsSubMenuOpen == true && + x.IsTopLevel == true && + x.HasSubMenu == true && + x.Parent == menu); + var e = new PointerPressedEventArgs { MouseButton = MouseButton.Left, Source = item }; + + target.PointerPressed(item, e); + Mock.Get(menu).Verify(x => x.Close()); + } + [Fact] public void PointerEnter_Opens_Item_When_Old_Item_Is_Open() {