简体   繁体   English

wpf上下文菜单左键单击

[英]wpf context menu left-click

Is it possible to attach context menu to a wpf control and have it opened on left-click (as opposed to more customary right-click)? 是否可以将上下文菜单附加到wpf控件,并在单击鼠标左键(而不是通常的右键单击)时将其打开? I want to achieve that using xaml only (this should be a part of my control's view template). 我想仅使用xaml来实现这一点(这应该是控件的视图模板的一部分)。

Here is a way to show context menu on left-click: 这是一种在单击鼠标左键时显示上下文菜单的方法:

Create a new left button handler on the Border element: Border元素上创建一个新的左按钮处理程序:

<Border x:Name="Win"
        Width="40"
        Height="40"
        Background="Purple"
        MouseLeftButtonUp="UIElement_OnMouseLeftButtonUp">

and then add this: 然后添加以下内容:

private void UIElement_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;

    var mouseDownEvent =
        new MouseButtonEventArgs(Mouse.PrimaryDevice,
            Environment.TickCount,
            MouseButton.Right)
        {
            RoutedEvent = Mouse.MouseUpEvent,
            Source = Win,
        };


    InputManager.Current.ProcessInput(mouseDownEvent);
}

What it does, it basically maps the left-click into right-click. 它所做的基本上是将左键单击映射为右键单击。 For reusability, you can wrap this into an attached behavior. 为了实现可重用性,可以将其包装为附加行为。

Here is how I would do a simple example of what I am suggesting: 这是我要提出的建议的简单示例:

The XAML: XAML:

<Window x:Class="LeftClickMenu.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
    <Grid>
        <Border Width="400" Height="300" Background="#ccc" BorderBrush="#333" 
                BorderThickness="1"
                MouseLeftButtonDown="Border_MouseLeftButtonDown"
                MouseRightButtonUp="Border_MouseRightButtonUp">
            <Border.ContextMenu>
                <ContextMenu x:Name="myContextMenu">
                    <MenuItem Header="Menu Item 1" />
                    <MenuItem Header="Menu Item 2" />
                    <MenuItem Header="Menu Item 3" />
                    <MenuItem Header="Menu Item 4" />
                    <MenuItem Header="Menu Item 5" />
                </ContextMenu>
            </Border.ContextMenu>
        </Border>
    </Grid>
</Window>

And the code-behind: 和背后的代码:

using System.Windows;
using System.Windows.Input;

namespace LeftClickMenu
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            myContextMenu.IsOpen = true;
        }

        private void Border_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }
    }
}

I also added the extra MouseRightButtonUp event to inhibit the right-click popup of the context menu. 我还添加了额外的MouseRightButtonUp事件,以禁止右键单击上下文菜单的弹出窗口。

  1. Create a method to programmatically open a submenu as stated in this SO article: Show menu programmatically in WPF 创建一种方法以编程方式打开此SO文章中所述的子菜单: 在WPF中以编程方式显示菜单

  2. Create an event for LeftMouseButtonDown and call that event in XAML. 为LeftMouseButtonDown创建一个事件,然后在XAML中调用该事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM