From 36f878b02ae2610ca070afbc73ca29e6335889f5 Mon Sep 17 00:00:00 2001 From: Tako Date: Mon, 14 Mar 2022 15:38:47 +0200 Subject: [PATCH] Fix "ContextMenu ignores styles on first open" --- src/Avalonia.Controls/ContextMenu.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/ContextMenu.cs b/src/Avalonia.Controls/ContextMenu.cs index 7a8438f1c3..bc5195ff6c 100644 --- a/src/Avalonia.Controls/ContextMenu.cs +++ b/src/Avalonia.Controls/ContextMenu.cs @@ -351,6 +351,11 @@ namespace Avalonia.Controls ? PlacementMode.Bottom : PlacementMode; + //Position of the line below is really important. + //All styles are being applied only when control has logical parent. + //Line below will add ContextMenu as child to the Popup and this will trigger styles and they would be applied. + //If you will move line below somewhere else it may cause that ContextMenu will behave differently from what you are expecting. + _popup.Child = this; _popup.PlacementTarget = placementTarget; _popup.HorizontalOffset = HorizontalOffset; _popup.VerticalOffset = VerticalOffset; @@ -359,7 +364,6 @@ namespace Avalonia.Controls _popup.PlacementGravity = PlacementGravity; _popup.PlacementRect = PlacementRect; _popup.WindowManagerAddShadowHint = WindowManagerAddShadowHint; - _popup.Child = this; IsOpen = true; _popup.IsOpen = true;