Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8641675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:35:16+00:00 2026-06-12T11:35:16+00:00

I have a canvas where a draw some GraphNodes and add them to the

  • 0

I have a canvas where a draw some GraphNodes and add them to the canvas as ContentControls. All graph nodes have an adorner which I use to draw connection lines from a node to another node. The adorner has a method OnMouseUp:

protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e)
{
    var SourceNode = AdornedElement;
    Point pt = PointToScreen(Mouse.GetPosition(this));
    var DestinationNode = ???
}

At this point I have the source node from where I started to draw the line in in AdornedElement which is the initial GraphNode. Also, I have the coordinates where the mouse was released. Under this point is another GraphNode. How to find the node that is under this point ?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:35:18+00:00Added an answer on June 12, 2026 at 11:35 am

    OK, after many research I have found a solution:

    protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e)
    { 
       Point pt = PointToScreen(Mouse.GetPosition(this));
       UIElement canvas = LogicalTreeHelper.GetParent(AdornedElement) as UIElement;
       // This is important to get the mouse position relative to the canvas, otherwise it won't work
       pt = canvas.PointFromScreen(pt);
       VisualTreeHelper.HitTest(canvas, null, HitTestResultCallbackHandler, new PointHitTestParameters(pt));            
    }
    

    And then the HitTest method to find the GraphNode. Remember that GraphNode is a custom object.

    public HitTestResultBehavior HitTestResultCallbackHandler(HitTestResult result)
    {
       if (result != null)
       {
          // Search for elements that have GraphNode as parent
          DependencyObject dobj = VisualTreeHelper.GetParent(result.VisualHit);
          while (dobj != null && !(dobj is ContentControl))
          {
             dobj = VisualTreeHelper.GetParent(dobj);
          }
          ContentControl cc = dobj as ContentControl;   
          if (!ReferenceEquals(cc, null))
          {
             IEnumerable<DependencyObject> dependencyObjects = cc.GetSelfAndAncestors();
             if (dependencyObjects.Count() > 0)
                {
                    IEnumerable<ContentControl> contentControls = dependencyObjects.Where(x => x.GetType() == typeof(ContentControl)).Cast<ContentControl>();
                    if (contentControls.Count() > 0)
                        {
                           ContentControl cControl = contentControls.FirstOrDefault(x => x.Content.GetType() == typeof(GraphNode));
                            if (cControl != null)
                            {
                                var graphNode = cControl.Content as GraphNode;
                                if (!ReferenceEquals(graphNode, null))
                                {
                                   // Keep the result in a local variable
                                   m_DestinationNode = graphNode;
                                   return HitTestResultBehavior.Stop;
                                }
                            }                           
                        }
                    }
                }               
            }
        m_DestinationNode = null;
        return HitTestResultBehavior.Continue;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some objects which I draw onto a Canvas as part of a
I have a large module written in JS which uses Canvas to draw and
I have a class which inherits from Canvas. On the OnRender method I draw
I have an isometric map which I draw to the canvas. When I try
I have a canvas in which I need to draw text on in javascript,
I have a View that was created at runtime then I draw some canvas
i have a JavaScript project (using jquery), inside it i draw a canvas by
I have the following code for writing draw calls to a back buffer canvas,
I have two points in the canvas, now I'm able to draw a line
How I can draw bezier curve in canvas. I have only start point and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.