diff --git a/src/Avalonia.Controls/Flyouts/FlyoutBase.cs b/src/Avalonia.Controls/Flyouts/FlyoutBase.cs index dfbd3f9a366..4801fa69f00 100644 --- a/src/Avalonia.Controls/Flyouts/FlyoutBase.cs +++ b/src/Avalonia.Controls/Flyouts/FlyoutBase.cs @@ -219,7 +219,7 @@ protected virtual bool ShowAtCore(Control placementTarget, bool showAtPointer = ((ISetLogicalParent)Popup).SetParent(null); } - if (Popup.PlacementTarget != placementTarget) + if (Popup.Parent == null || Popup.PlacementTarget != placementTarget) { Popup.PlacementTarget = Target = placementTarget; ((ISetLogicalParent)Popup).SetParent(placementTarget); diff --git a/tests/Avalonia.Controls.UnitTests/FlyoutTests.cs b/tests/Avalonia.Controls.UnitTests/FlyoutTests.cs index 8b77074960d..776b4508c20 100644 --- a/tests/Avalonia.Controls.UnitTests/FlyoutTests.cs +++ b/tests/Avalonia.Controls.UnitTests/FlyoutTests.cs @@ -451,6 +451,28 @@ public void Should_Reset_Popup_Parent_On_Target_Detached() Assert.Null(popup.Parent); } } + + [Fact] + public void Should_Reset_Popup_Parent_On_Target_Attach_Following_Detach() + { + using (CreateServicesWithFocus()) + { + var userControl = new UserControl(); + var window = PreparedWindow(userControl); + window.Show(); + + var flyout = new TestFlyout(); + flyout.ShowAt(userControl); + + var popup = Assert.IsType(flyout.Popup); + Assert.NotNull(popup.Parent); + + flyout.Hide(); + + flyout.ShowAt(userControl); + Assert.NotNull(popup.Parent); + } + } [Fact] public void ContextFlyout_Can_Be_Set_In_Styles()