开发者

How can I right-align accelerator text in a Windows.Controls.ContextMenu menuitem?

开发者 https://www.devze.com 2023-01-29 08:46 出处:网络
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.W

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消