在 WPF 中创建拖动完成时的上下文菜单

发布于 2024-10-04 10:34:56 字数 358 浏览 0 评论 0原文

我正在开发类似于 Maya 的 HyperGraph 的节点图视图,在其中我可以通过拖放连接节点。因为目标节点可以有多个输入,所以我想创建一个临时上下文菜单来选择以下模型中建议的输入:

http://www.pixtur.org/images/uploaded/0000/0696/large.jpg

我尝试了很长一段时间来触发创建或打开上下文菜单的。看起来 Win32 TrackPopupMenu 大致可以实现我正在寻找的功能。是否有 WPF/C# 等效项?

谢谢 像素图

I am working on a node-graph-view similar to Maya's HyperGraph in which I can connect Nodes with drag and drop. Because the target-node can have several Inputs, I want to create a temporary ContextMenu to select the input as suggesting in the following mock-up:

http://www.pixtur.org/images/uploaded/0000/0696/large.jpg

I tried for quite a time to trigger the creation or opening of a context-menu. It looks like the Win32 TrackPopupMenu does roughly, what I'm looking for. Is there an WPF / C# equivalent?

Thanks
pixtur

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

谈情不如逗狗 2024-10-11 10:34:56

我建议另一种解决方案:

在此示例中,右键单击按钮将弹出一个上下文菜单,其中包含一个条目(“复制”)。如果单击“复制”上下文菜单项,则会生成控制台输出。

[..]
var button = new Button();
button.Content = "SomeButtonName";
button.MouseUp += HandleMouseUp;
[..]


private void HandleMouseUp(object sender, MouseButtonEventArgs e)
{
    var senderUIControl = sender as Control;

    var contextMenu = new ContextMenu();

    var item = new MenuItem();
    item.Header = "Copy";
    item.Click += (o, a) => {
        Console.WriteLine("Copy item clicked");
    };
    contextMenu.Items.Add(item);

    senderUIControl.ContextMenu = contextMenu;
}

I would suggest another solution:

In this example a button will raise a context menu with one entry ("Copy") on right click. If the "Copy" context menu item is clicked, a console output is generated.

[..]
var button = new Button();
button.Content = "SomeButtonName";
button.MouseUp += HandleMouseUp;
[..]


private void HandleMouseUp(object sender, MouseButtonEventArgs e)
{
    var senderUIControl = sender as Control;

    var contextMenu = new ContextMenu();

    var item = new MenuItem();
    item.Header = "Copy";
    item.Click += (o, a) => {
        Console.WriteLine("Copy item clicked");
    };
    contextMenu.Items.Add(item);

    senderUIControl.ContextMenu = contextMenu;
}
放血 2024-10-11 10:34:56

我使用以下代码将上下文菜单附加到列表视图 gricolumn 标题:

<ListView ... MouseUp="ListView_MouseUp">

在代码隐藏中,我在鼠标向上事件上设置列表的 ContextMenu 属性,以便显示上下文菜单:

    private void ListView_MouseUp(object sender, MouseButtonEventArgs e)
    {
        DependencyObject depObj = e.OriginalSource as DependencyObject;

        while (depObj != null && (!(depObj is GridViewColumnHeader)))
        {
            depObj = VisualTreeHelper.GetParent(depObj);
        }            

        if (depObj is GridViewColumnHeader && e.ChangedButton == MouseButton.Left)
        {
            ((GridViewColumnHeader)depObj).ContextMenu = ContextMenu;
        }
    }

变量 ContextMenu指的是我之前创建的上下文菜单实例,您也可以在鼠标事件处理程序中创建上下文菜单。
我不确定这是否有帮助,因为我不知道你如何进行拖放,但值得一试

I use the following code to attach a contextmenu to a listview gricolumn header:

<ListView ... MouseUp="ListView_MouseUp">

In the codebehind i set the ContextMenu property of the list on the mouse up event, in order to show the context menu:

    private void ListView_MouseUp(object sender, MouseButtonEventArgs e)
    {
        DependencyObject depObj = e.OriginalSource as DependencyObject;

        while (depObj != null && (!(depObj is GridViewColumnHeader)))
        {
            depObj = VisualTreeHelper.GetParent(depObj);
        }            

        if (depObj is GridViewColumnHeader && e.ChangedButton == MouseButton.Left)
        {
            ((GridViewColumnHeader)depObj).ContextMenu = ContextMenu;
        }
    }

The variable ContextMenu refers to a contextmenu instance that i created bfeorehand, you could also create the ContextMenu in the Mouse event handler.
I'm not sure if this helps as I dont know how you do the drag/drop, but it is worth a try

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