同时使用两个上下文菜单进行多点触控
如何同时在窗口上显示两个 ContextMenu
?
这是我的课
public partial class Window1 : Window
{
ContextMenu contextMenu1 = new ContextMenu();
ContextMenu contextMenu2 = new ContextMenu();
public Window1()
{
InitializeComponent();
contextMenu1.Items.Add("Hello1");
contextMenu2.Items.Add("Hello2");
contextMenu1.Placement = System.Windows.Controls.Primitives.PlacementMode.Relative;
contextMenu2.Placement = System.Windows.Controls.Primitives.PlacementMode.Relative;
contextMenu1.PlacementTarget = rectangle1;
contextMenu2.PlacementTarget = rectangle2;
contextMenu1.StaysOpen = true;
contextMenu2.StaysOpen = true;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
contextMenu1.IsOpen = true;
contextMenu2.IsOpen = true;
}
}
How can I show two ContextMenu
on my window at the same time?
this is my class
public partial class Window1 : Window
{
ContextMenu contextMenu1 = new ContextMenu();
ContextMenu contextMenu2 = new ContextMenu();
public Window1()
{
InitializeComponent();
contextMenu1.Items.Add("Hello1");
contextMenu2.Items.Add("Hello2");
contextMenu1.Placement = System.Windows.Controls.Primitives.PlacementMode.Relative;
contextMenu2.Placement = System.Windows.Controls.Primitives.PlacementMode.Relative;
contextMenu1.PlacementTarget = rectangle1;
contextMenu2.PlacementTarget = rectangle2;
contextMenu1.StaysOpen = true;
contextMenu2.StaysOpen = true;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
contextMenu1.IsOpen = true;
contextMenu2.IsOpen = true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,当单击上下文菜单本身以外的其他位置时,上下文菜单将会消失。因此,同时打开两个上下文菜单将涉及一些特殊处理。
除此之外,这种设置很可能会让用户感到困惑。
也许您应该考虑其他选项(工具栏、属性面板、级联上下文菜单)
编辑
现在您已将代码添加到问题中,我更加担心这种方法。您想向用户提供什么?您应该简单地使用 Grids 或 DockPanels 而不是矩形,并向其中添加常规菜单:
Normally a context menu will disappear when clicking somewhere else than in the context menu itself. So having two context menus open at the same time will involve some special handling.
Besides that, this setup will most probably cause the user to be confused.
Perhaps you should consider an other option (toolbar, property panel, cascading context menu)
EDIT
Now that you added your code to the question I am even more worried about this approach. What are you trying to offer to the user? You should simply use Grids or DockPanels instead of rectangles and add regular Menus to them:
您可以使用 Popup 控件捕获右键单击事件以显示它。
不幸的是,您将从头开始,并且必须重建上下文菜单,但同时打开两个菜单不会有任何问题。
You can use the Popup control and catch the right-click event to display it.
Unfortunately you'll be starting from scratch and will have to rebuild the context menu, but there will be no problems with having two open at once.
在不知道很多关于您想要做的细节的情况下 - 这就是 WPF!所以你可以按照你想要的方式设计一切。
您可以通过单击鼠标右键来显示对话框/用户控件/Windows,并将它们设置为上下文菜单的样式或它们应该看起来的样子。这样您就可以完全控制要显示的内容......
Without knowing alot of Details about you want to do - it is WPF! So you can style everything like you want.
You can Display Dialogs/UserControls/Windows on right mouse click and style them like the context menu or however they should look like. With this you have the full control of what you want to display...