2012-11-26 50 views
1

我想打一個ContextMenu,當用戶進行「右鍵點擊」到contorol,這是一個按鈕,如圖。不幸的是一些按鈕被禁用。有人能幫助我,並告訴我如何給他們一個contextmenu嗎?文本菜單爲禁用的按鈕

我(不工作)嘗試:

 private void ShowRightClickMenu(object sender, MouseEventArgs e) 
    { 
     ContextMenu Temp = new ContextMenu(); 
     if (e.Button == MouseButtons.Right && secondTagObj[Convert.ToInt32(((Button)sender).Tag)].typ != string.Empty) 
     { 
      this.ContextMenu = Temp;  // works 
      Temp.MenuItems.Add("Create.."); //works 
      Temp.MenuItems.Add("Delete"); // works 
     } 
     if (raster[Convert.ToInt32(((Button)sender).Tag)].Enabled == false && e.Button == MouseButtons.Right) 
     { 
      this.ContextMenu = Temp;  // works not 
      Temp.MenuItems.Add("New..."); // works not 
     } 
     else 
     { 
      this.ContextMenu = Temp;  // works, but only if button is visible 
      Temp.MenuItems.Add("New..."); // works, but only if button is visible 
     } 
    } 

提前非常感謝。

回答

1

在WPF中你可以使用ContextMenuService啓用禁用控制文本菜單。

private void ShowRightClickMenu(object sender, MouseEventArgs e) 
{ 
    ContextMenu Temp = new ContextMenu(); 
    ContextMenuService.SetShowOnDisabled((Button)sender, true); 
... 

[更新更好的readabiltiy] 對於的WinForms看看this entry at microsoft forums

希望這有助於。

+0

不幸的是它是一個正常的形式,而不是一個WPF,所以我不能使用System.Windows.Controls。但謝謝:) – BudBrot

+0

嗯that's太糟糕了,但你可以看看這個鏈接,也許這可以幫助你:[鏈接](http://social.msdn.microsoft.com/Forums/en-US/ winforms/thread/2977f3ae-5522-4ba5-a52e-133feebd51d4 /) –

+0

謝謝, – BudBrot