2014-09-03 100 views
0

我有datagrid與上下文菜單。它以編程方式初始化:點擊後保持打開的上下文菜單

contextMenu = new ContextMenu(); 

foreach (var col in this.Columns) 
{ 
    var checkBox = new MenuItem() 
    { 
     Header = col.Header 
    }; 
    Binding myBinding = new Binding("Visibility"); 
    myBinding.Mode = BindingMode.TwoWay; 
    myBinding.Converter = new IsCheckedToVisibilityConverter(); 
    checkBox.DataContext = col; 
    checkBox.SetBinding(MenuItem.IsCheckedProperty, myBinding); 
    checkBox.Click += checkBox_Click; 
    checkBox.Checked += checkBox_Checked; 
    checkBox.Unchecked += checkBox_Unchecked; 
    contextMenu.Items.Add(checkBox); 

} 

它很好,但我想保持打開上下文菜單後檢查\ uncheck menuitems。有任何想法嗎 ?

回答

2

加入checkBox.StaysOpenOnClick = true;按預期工作

contextMenu = new ContextMenu(); 

      foreach (var col in this.Columns) 
      { 
       var checkBox = new MenuItem() 
       { 
        Header = col.Header 
       }; 
       //binding 
       Binding myBinding = new Binding("Visibility"); 
       myBinding.Mode = BindingMode.TwoWay; 
       myBinding.Converter = new IsCheckedToVisibilityConverter(); 
       checkBox.DataContext = col; 
       checkBox.SetBinding(MenuItem.IsCheckedProperty, myBinding); 
       checkBox.Click += checkBox_Click; 
       checkBox.Checked += checkBox_Checked; 
       checkBox.Unchecked += checkBox_Unchecked; 
       checkBox.StaysOpenOnClick = true; 
       contextMenu.Items.Add(checkBox); 

      } 
0

你可以試試:

private bool close= true; 

private void CheckBox1_CheckedChanged(Object sender, EventArgs e) 
{ 
close= false; 
} 

private void contextMenu_Closing(object sender, ToolStripDropDownClosingEventArgs e) 
{ 
    e.Cancel = !close; 
    CloseContextMenu = true; 
} 
+0

對不起後,但我怎麼能趕上文本菜單關閉事件? – maskalek 2014-09-04 06:47:59

+0

該死的我忘記了contextmenu沒有關閉事件。那麼也許Collapse事件會起作用,但我不確定。 http://www.vbforums.com/showthread.php?551802-RESOLVED-2008-ContextMenu-Closed-Event – HeadShotSmiley 2014-09-04 09:26:46

相關問題