This is a migrated thread and some comments may be shown as answers.

How to identify specific node right clicked on for contextmenu?

1 Answer 196 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 19 Jun 2015, 04:29 PM

Hello,

I need some advice on an approach to have a context menu associated for a node that is right-clicked on.  There is a way to associate a context menu with the entire diagram, but I cannot find a clean way to associate it with a specific node.  I have tried the following:

  1. diagram ShapeClicked event does not fire for right clicking on a node
  2. diagram.MouseRightButtonDown even only returns position, so I would have to iterate over all nodes to find one at the specific position
  3. I've tried adding a RadContextMenu to a DataTemplate for a node, but this seems a messy hack since I would need to ad a ui component that fills the node's body simply to have a context menu.
  4. I even looked at adding mouse-over behavior to show a pop-up menu on a node (similar to the mindmap demo), but the user requires other behavior on mouse-over and so such a menu would be an impractical approach.

What is the preferred approach of creating a context menu for a node in the diagram?

Thanks in advance,

Mike

 

 

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 23 Jun 2015, 02:03 PM
Hello Mike,

You can achieve your requirement using the MouseRightButtonDown event of the diagram and its HitTestService. Basically, you can get the shape under the mouse inside the event handler using the service and execute your logic there. Here is an example for this approach:
<telerik:RadDiagram x:Name="diagram" MouseRightButtonDown="diagram_MouseRightButtonDown">
    <telerik:RadContextMenu.ContextMenu>
        <telerik:RadContextMenu>                   
        </telerik:RadContextMenu>
    </telerik:RadContextMenu.ContextMenu>
</telerik:RadDiagram>      

private HitTestService hitTestService;
public MainPage()
{
    InitializeComponent();
    this.hitTestService = this.diagram.ServiceLocator.GetService<IHitTestService>() as HitTestService;
}       
 
private void diagram_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    var shapeUnderMouse = this.hitTestService.ShapeUnderMouse;
    if (shapeUnderMouse != null)
    {
        // create the menu items for the shape under the mouse
     // or execute any other logic
    }
}

Please try this approach and let me know if it works for you.

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Diagram
Asked by
Michael
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or