【问题标题】:Stay open context menu after click单击后保持打开上下文菜单
【发布时间】:2014-09-03 10:16:32
【问题描述】:

我有带有上下文菜单的数据网格。它以编程方式初始化:

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);

}

效果很好,但我想在选中\取消选中菜单项后保持打开上下文菜单。有什么想法吗?

【问题讨论】:

    标签: c# wpf contextmenu


    【解决方案1】:

    添加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);
    
                }
    

    【讨论】:

      【解决方案2】:

      你可以试试:

      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;
      }
      

      【讨论】:

      • 对不起,我怎样才能捕捉到上下文菜单关闭事件??
      • 该死的我忘了上下文菜单没有关闭事件。那么也许 Collapse 事件会起作用,但我不确定。 vbforums.com/…
      猜你喜欢
      • 1970-01-01
      • 2014-03-25
      • 2011-02-17
      • 1970-01-01
      • 2012-08-30
      • 2013-11-13
      • 2012-05-15
      • 1970-01-01
      • 2014-05-01
      相关资源
      最近更新 更多