How to identify specific node right clicked on for contextmenu?

2 posts, 0 answers
  1. Michael
    Michael avatar
    26 posts
    Member since:
    Oct 2014

    Posted 19 Jun 2015 Link to this post

    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

     

     

  2. Martin
    Admin
    Martin avatar
    1099 posts

    Posted 23 Jun 2015 Link to this post

    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
  3. DevCraft banner
Back to Top