右键单击可保持ContextMenu打开

时间:2018-09-01 10:02:13

标签: c# wpf

我有一个带有上下文菜单的按钮。鼠标左键打开此上下文菜单。问题是,使用右键单击按钮时,上下文菜单关闭。我要禁用右键单击按钮。

        private void NotificationBtn_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
           if (e.ChangedButton == MouseButton.Right)
           {
              e.Handled = true;
              return;
           }

           if (e.ChangedButton == MouseButton.Left)
           {
             (sender as Button).ContextMenu.IsEnabled = true;
             (sender as Button).ContextMenu.PlacementTarget = (sender as Button);
             (sender as Button).ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
             (sender as Button).ContextMenu.IsOpen = true;    
            }
         }

    private void NotificationBtn_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Right)
        {
            e.Handled = true;
        }
    }

使用鼠标右键单击按钮时如何保持此上下文菜单打开?

0 个答案:

没有答案