我正在动态创建一个子菜单,但是我发现_
菜单项助记符不能触发菜单命令。使用alt
菜单导航打开菜单时,项目显示下划线(即,这在我的窗口菜单下):
var mi = Model.Window.MainMenuWindow;
mi.Items.Clear();
mi.Items.Add(new MenuItem { Header = "_Close Document", Command=Model.Commands.CloseActiveDocumentCommand });
mi.Items.Add(new MenuItem { Header = "C_lose All Documents", Command=Model.Commands.CloseAllDocumentsCommand, CommandParameter = "All" });
mi.Items.Add(new MenuItem { Header = "Close All _But This Document", Command = Model.Commands.CloseAllDocumentsCommand, CommandParameter="AllBut" });
var menuItems = Model.Window.GenerateContextMenuItemsFromOpenTabs();
if (menuItems.Count < 1)
return;
mi.Items.Add(new Separator());
foreach (var menu in menuItems)
{
mi.Items.Add(menu);
}
mi.IsSubmenuOpen = true;
这是它的样子:
助记符属性显示快捷方式,但是热键无法触发相关命令。
当动态添加项目以使快捷方式链接有效时,菜单中是否存在需要特殊的内容?
FWIW,我正在使用MahApps.Metro,但我怀疑这并不是真的相关。
答案 0 :(得分:1)
在菜单上设置焦点将解决此问题。
void ShowActiveTabsList()
{
WindowMenuCommand = new CommandBase((parameter, command) =>
{
var mi = Model.Window.MainMenuWindow;
mi.Items.Clear();
mi.Items.Add(new MenuItem { Header = "_Close Document", Command= Model.Commands.CloseActiveDocumentCommand });
mi.Items.Add(new MenuItem { Header = "Close _All Documents", Command = Model.Commands.CloseAllDocumentsCommand });
mi.Items.Add(new MenuItem { Header = "Close All _But This Document", Command = Model.Commands.CloseAllDocumentsCommand, CommandParameter="AllBut" });
var menuItems = Model.Window.GenerateContextMenuItemsFromOpenTabs();
if (menuItems.Count < 1)
return;
mi.Items.Add(new Separator());
foreach (var menu in menuItems)
{
mi.Items.Add(menu);
}
mi.IsSubmenuOpen = true;
// focus the menu to enable the mnemonics for the dynamically created submenus
mi.Focus();
mi.SubmenuClosed += (s,e) => ((MenuItem)s).Items.Clear();
}, (p, c) => true);
}