单击鼠标左键打开上下文菜单 wpf c#
Open contextmenu on left mouse click wpf c#
我有一个 rectangle.The rectangle
有一个自定义 contextmenu
(只是在 <ContextMenu.Template>
的 ControlTemplae
中做了一些简单的更改)。我想要什么就是,鼠标左键点击,contextmenu
会弹出。
我尝试在矩形的 MouseDown
event.Yes 中添加 rectangle1.contextmenu.isopen=true
,它打开 contextmenu
。但是,contextmenu
设置为 open/pop 在矩形的上方(顶部),如果我在矩形的 MouseDown
事件中使用 rectangle1.contextmenu.isopen=true
,我只需将 ContextMenuService.Placement="top"
添加到矩形的 XAML.But 即可,然后contextmenu
弹出但在错误的位置,它不再位于顶部,而是跟随 mouse.E.g。如果我单击矩形的右角,right.This 中的 contextmenu
opens/pops 行为非常奇怪,我不知道为什么会这样。
无论如何,如何在鼠标左键单击时打开矩形顶部的 contextmenu
?
更新
奇怪的是,无论我在 mouseevent
中添加什么代码,上下文菜单都会丢失它的位置! E.g.If 我什至在 mouseDown 事件上添加 MsgBox("abc")
,然后右键单击矩形,上下文菜单不在顶部!!
我想这就是你想要的?
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;
正如我从 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. If the user opens the ContextMenu by using the
mouse, Placement is set to MousePoint. If the user opens the
ContextMenu by using the keyboard, Placement is set to Center. If you
want to change the position of the ContextMenu, set the
ContextMenuService.Placement property on the FrameworkElement or
FrameworkContentElement.
因此,由于您不是通过 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;
}
我有一个 rectangle.The rectangle
有一个自定义 contextmenu
(只是在 <ContextMenu.Template>
的 ControlTemplae
中做了一些简单的更改)。我想要什么就是,鼠标左键点击,contextmenu
会弹出。
我尝试在矩形的 MouseDown
event.Yes 中添加 rectangle1.contextmenu.isopen=true
,它打开 contextmenu
。但是,contextmenu
设置为 open/pop 在矩形的上方(顶部),如果我在矩形的 MouseDown
事件中使用 rectangle1.contextmenu.isopen=true
,我只需将 ContextMenuService.Placement="top"
添加到矩形的 XAML.But 即可,然后contextmenu
弹出但在错误的位置,它不再位于顶部,而是跟随 mouse.E.g。如果我单击矩形的右角,right.This 中的 contextmenu
opens/pops 行为非常奇怪,我不知道为什么会这样。
无论如何,如何在鼠标左键单击时打开矩形顶部的 contextmenu
?
更新
奇怪的是,无论我在 mouseevent
中添加什么代码,上下文菜单都会丢失它的位置! E.g.If 我什至在 mouseDown 事件上添加 MsgBox("abc")
,然后右键单击矩形,上下文菜单不在顶部!!
我想这就是你想要的?
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;
正如我从 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. If the user opens the ContextMenu by using the mouse, Placement is set to MousePoint. If the user opens the ContextMenu by using the keyboard, Placement is set to Center. If you want to change the position of the ContextMenu, set the ContextMenuService.Placement property on the FrameworkElement or FrameworkContentElement.
因此,由于您不是通过 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;
}