简体   繁体   English

如何在Windows.Controls.ContextMenu菜单项中右对齐加速器文本?

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

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" ). 在Win32 API中,制表符( \\t )用于在菜单项( "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; 在C#应用程序中,我有一个派生自System.Windows.Controls.ContextMenu的类,并且看起来以类似方式使用制表符不能达到相同的结果。 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. 我知道在.net中_代替Win32 &用于助记符下划线。 Is there a similar substitute for \\t ? \\t是否有类似的替代品?

Edit: code for context (without the ICommand implementation) 编辑:上下文代码(没有ICommand实现)

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(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. 将加速器文本设置为MenuItem.InputGestureText属性。

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. 有关如何将命令与菜单项关联的信息,请参见命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM