diff --git a/src/Avalonia.Controls/ContextMenu.cs b/src/Avalonia.Controls/ContextMenu.cs index 57e4909e39a..395f235b610 100644 --- a/src/Avalonia.Controls/ContextMenu.cs +++ b/src/Avalonia.Controls/ContextMenu.cs @@ -386,14 +386,19 @@ private void PopupClosed(object sender, EventArgs e) private static void ControlPointerReleased(object sender, PointerReleasedEventArgs e) { var control = (Control)sender; - var contextMenu = control.ContextMenu; + var contextMenu = control?.ContextMenu; - if (control.ContextMenu.IsOpen) + if (control == null || contextMenu == null) + { + return; + } + + if (contextMenu.IsOpen) { if (contextMenu.CancelClosing()) return; - control.ContextMenu.Close(); + contextMenu.Close(); e.Handled = true; }