diff --git a/src/Consolonia.Themes.TurboVision/Templates/Controls/Helpers/MenuExtensions.cs b/src/Consolonia.Themes.TurboVision/Templates/Controls/Helpers/MenuExtensions.cs index decb5eec..28f0713b 100644 --- a/src/Consolonia.Themes.TurboVision/Templates/Controls/Helpers/MenuExtensions.cs +++ b/src/Consolonia.Themes.TurboVision/Templates/Controls/Helpers/MenuExtensions.cs @@ -38,13 +38,17 @@ static MenuExtensions() var focusedControl = (Control)AvaloniaLocator.Current.GetRequiredService()! .GetFocusedElement(); - var menuItems = visual.GetLogicalAncestors().OfType(); - var focusedTree = focusedControl!.GetLogicalAncestors(); + if (focusedControl != null) + { + var focusedTree = focusedControl.GetLogicalAncestors(); + var menuItems = visual.GetLogicalAncestors().OfType(); - foreach (MenuItem menuItem in menuItems.Where(item => !focusedTree.Contains(item)) - .ToArray()) - menuItem.Close(); + foreach (MenuItem menuItem in menuItems + .Where(item => !focusedTree.Contains(item)) + .ToArray()) + menuItem.Close(); + } }); })); visual.SetValue(DisposablesProperty, new[] { disposable });