开发者

Creating a context menu on drag complete in WPF

开发者 https://www.devze.com 2023-01-26 19:28 出处:网络
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 ContextMe

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 co开发者_JAVA技巧ntext-menu. It looks like the Win32 TrackPopupMenu does roughly, what I'm looking for. Is there an WPF / C# equivalent?

Thanks pixtur


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;
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消