In the Win32 API, the tab character (\t
) is used to display right-aligned text (like for accelerators / shortcuts) in a menu item ("Open\tCtrl+O"
). In a C# app, I have a class derived from System.Windows.Controls.ContextMenu
and it appears that using the tab character in a similar manner does not achieve the same result; it actually inserts a tab, so the shortcut looks more center-aligned than right-aligned.
I know that in .net _
is used in place of the Win32 &
for mnemonic underlines. Is there a similar substitute for \t
?
Edit: code for context (without the ICommand implementation)
internal class MyContextMenu : ContextMenu, ICommand
{
private readonly string[] wordList;
public MyContextMenu(string aWord)
{
var itemStyle = (Style) TryFindResource("EditorContextMenuItem");
wordList = GetMyWordList(aWord);
if (wordList != null)
{
for (int i = 0; i < wordList.Length; ++i)
{
string word = wordList[i];
var item = new MenuItem
{
Style = itemStyle,
Header = BuildMenuText开发者_StackOverflow中文版(i + 1, word),
Command = this,
CommandParameter = i
};
this.Items.Add(item);
}
}
}
static private string BuildMenuText(int index, string text)
{
string menuText;
if (index > 0 && index < 16)
menuText = text + "\t_" + index.ToString("x");
else
menuText = "_" + text;
return menuText;
}
}
Set your accelerator text to the MenuItem.InputGestureText property.
Also, note the remark in the documentation page: This property does not associate the input gesture with the menu item; it simply adds text to the menu item. The application must handle the user's input to carry out the action. For information on how to associate a command with a menu item, see Command.
精彩评论