本文介绍了左键单击wpf c#打开上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个矩形。"矩形"有一个自定义的`contextmenu`(只是在`< ContextMenu.Template>`的`ControlTemplae`中进行的一些简单的更改。)我想要的是,在鼠标左键单击时, `contextmenu`将弹出。 我尝试在矩形的'MouseDown`事件中添加`rectangle1.contextmenu.isopen = true`。是的,它会打开`contextmenu `。但是,`contextmenu`设置为在矩形的上方(顶部)打开/弹出,我通过简单地将`ContextMenuService.Placement =" top"`添加到矩形的XAML.But来实现它。如果我在矩形的'MouseDown`事件中使用`rectangle1.contextmenu.isopen = true`,那么`contextmenu`会弹出,但是在错误的地方,它不再保持在顶部,而是跟在鼠标之后.Eg如果我点击矩形的右上角,"contextmenu"会在右边打开/弹出。这种行为很奇怪,我不知道为什么会发生这种情况。 无论如何,如何在鼠标左键单击矩形顶部打开`contextmenu`? $ **更新** 奇怪的是,无论我添加到任何"mouseevent"的代码,上下文菜单都会丢失它的位置!例如,我甚至在mouseDown事件中添加`MsgBox(" abc")`,然后右键单击矩形,上下文菜单不在顶部!! 解决方案 这是Windows的默认设置。 我已经阅读了很多关于这个问题的内容,无论你设置什么位置,它都会显示鼠标指针的位置。 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.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.E.g. 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.Anyway,how do i open the `contextmenu` at the top of the rectangle on left mouse click?**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 ! E.g.If i even add `MsgBox("abc")` on mouseDown event, and then right click on the rectangle, the context menu is not on top!! 解决方案 This is the default for Windows. I have read lots about this problem and no matter what you set the placement to it will show up where the mouse pointer is. 这篇关于左键单击wpf c#打开上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 21:51