简体   繁体   English

打开鼠标左键单击上下文菜单WPF C#

[英]Open contextmenu on left mouse click wpf c#

I have a rectangle.The rectangle has a custom contextmenu (just some simple changes made within the ControlTemplae of <ContextMenu.Template> ).What i want is,on left mouse click,the contextmenu will popup. 我有一个矩形。 rectangle有一个自定义的contextmenu (只是在<ContextMenu.Template>ControlTemplae中进行的一些简单更改)。我想要的是,在鼠标左键单击时将弹出该contextmenu <ContextMenu.Template>

I tried adding rectangle1.contextmenu.isopen=true in the rectangle's MouseDown event.Yes,it opens the contextmenu .However, the contextmenu is set to open/pop up above(on top) of the rectangle,i did it by simply adding ContextMenuService.Placement="top" to the rectangle's XAML.But if i use rectangle1.contextmenu.isopen=true in the rectangle's MouseDown event, then the contextmenu pops up but in the wrong place,it doesn't stay on top any more, rather it follows the mouse.Eg If i click the right corner of the rectangle,the contextmenu opens/pops up in the right.This behaviour is very strange,i don't know why this is happening. 我尝试添加rectangle1.contextmenu.isopen=true在矩形的MouseDown event.Yes,它打开contextmenu 。不过,该contextmenu设置为开/矩形上方弹出(顶部),我做到了通过简单地添加ContextMenuService.Placement="top"到矩形的XAML.But如果我使用rectangle1.contextmenu.isopen=true在矩形的MouseDown事件,然后将contextmenu弹出,但在错误的地方,它不会留在上面的任何更多,而它遵循mouse.Eg如果我点击矩形的右上角,在contextmenu打开在right.This行为/弹出很奇怪,我不知道为什么会这样。

Anyway,how do i open the contextmenu at the top of the rectangle on left mouse click? 无论如何,如何单击鼠标左键打开矩形顶部的contextmenu

UPDATE 更新

What's strange is that no matter what code i add to any of the mouseevent s,the context menu loses it's placement ! 奇怪的是,无论我将任何代码添加到任何mouseevent ,上下文菜单都将丢失它的位置! EgIf i even add MsgBox("abc") on mouseDown event, and then right click on the rectangle, the context menu is not on top!! 例如,如果我甚至在mouseDown事件上添加MsgBox("abc") ,然后右键单击矩形,则上下文菜单不在顶部!

As I can see from MSDN reference ContextMenu.Placement 从MSDN参考ContextMenu.Placement中可以看到

When the ContextMenu is assigned to the FrameworkElement.ContextMenu or FrameworkContentElement.ContextMenu property, the ContextMenuService changes this value of this property when the ContextMenu opens . 将ContextMenu分配给FrameworkElement.ContextMenu或FrameworkContentElement.ContextMenu属性时, 当ContextMenu打开时ContextMenuService会更改此属性的此值 If the user opens the ContextMenu by using the mouse, Placement is set to MousePoint. 如果用户使用鼠标打开ContextMenu,则Placement设置为MousePoint。 If the user opens the ContextMenu by using the keyboard, Placement is set to Center. 如果用户使用键盘打开ContextMenu,则Placement设置为Center。 If you want to change the position of the ContextMenu, set the ContextMenuService.Placement property on the FrameworkElement or FrameworkContentElement. 如果要更改ContextMenu的位置,请在FrameworkElement或FrameworkContentElement上设置ContextMenuService.Placement属性。

So since you do it not via ContextMenuService you should change Placement and PlacementTarget by yourself. 因此,由于您不是通过ContextMenuService进行操作,因此您应该自己更改Placement和PlacementTarget。

private void Mouse_Down(object sender, MouseButtonEventArgs e)
{
    var cm = ContextMenuService.GetContextMenu(sender as DependencyObject);
    if (cm==null)
    {
        return;
    }
    cm.Placement = PlacementMode.Top;
    cm.PlacementTarget = sender as UIElement;
    cm.IsOpen = true;
}

I think this is what you're going for? 我想这就是你要做什么?

rect.ContextMenu.PlacementTarget = rect;

rect.ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Top;
rect.ContextMenu.IsOpen = true;

// if you want it to be at the top and come down over the rectangle
rect.ContextMenu.VerticalOffset = rect.ContextMenu.ActualHeight;

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

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