同时使用两个上下文菜单进行多点触控

发布于 2024-10-30 15:58:14 字数 977 浏览 1 评论 0原文

如何同时在窗口上显示两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰◇生如夏花灿烂 2024-11-06 15:58:14

通常,当单击上下文菜单本身以外的其他位置时,上下文菜单将会消失。因此,同时打开两个上下文菜单将涉及一些特殊处理。

除此之外,这种设置很可能会让用户感到困惑。

也许您应该考虑其他选项(工具栏、属性面板、级联上下文菜单)

编辑

现在您已将代码添加到问题中,我更加担心这种方法。您想向用户提供什么?您应该简单地使用 Grids 或 DockPanels 而不是矩形,并向其中添加常规菜单:

    <DockPanel Width="200"
               Height="200">
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="Hello1" />
        </Menu>
        <Grid />
    </DockPanel>
    <DockPanel Width="200"
               Height="200">
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="Hello2" />
        </Menu>
        <Grid />
    </DockPanel>
</StackPanel>

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:

    <DockPanel Width="200"
               Height="200">
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="Hello1" />
        </Menu>
        <Grid />
    </DockPanel>
    <DockPanel Width="200"
               Height="200">
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="Hello2" />
        </Menu>
        <Grid />
    </DockPanel>
</StackPanel>
笑忘罢 2024-11-06 15:58:14

您可以使用 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.

惟欲睡 2024-11-06 15:58:14

在不知道很多关于您想要做的细节的情况下 - 这就是 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文