2010-12-09 14 views
2

在Win32 API中,製表符(\t)用於顯示菜單項("Open\tCtrl+O")中的右對齊文本(如加速器/快捷方式)。在C#應用程序中,我有一個從System.Windows.Controls.ContextMenu派生的類,並且似乎以類似方式使用製表符不會達到相同的結果;它實際上插入了一個選項卡,因此該快捷方式看起來比右對齊更加居中對齊。如何在Windows.Controls.ContextMenu menuitem中右對齊加速器文本?

我知道使用.net _來代替Win32 &的助記符下劃線。是否有類似\t的替代品?

編輯:上下文代碼(不執行的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; 
    } 
} 
+0

您是否嘗試過重新模板化ContextMenu項目?發佈一些代碼會很有幫助。 – decyclone 2010-12-09 06:21:48

回答

1

設置你的油門文本到MenuItem.InputGestureText財產。

此外,請注意文檔頁面中的註釋:此屬性不會將輸入手勢與菜單項相關聯;它只是添加文本到菜單項。應用程序必須處理用戶的輸入以執行該操作。有關如何將命令與菜單項關聯的信息,請參閱命令。