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

  • SEARCH
  • Home
  • 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 8977657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:25:19+00:00 2026-06-15T19:25:19+00:00

I have a Canvas with a couple of elements on it like Line, Path

  • 0

I have a Canvas with a couple of elements on it like Line, Path and Text Box. In the MouseOver event of the Canvas there’s a HitTest for all of them like:

bool HitTest( Point p )
{
  return VisualTreeHelper.HitTest( textBox, p ) != null;
}

This works pixel-accurate for Line and Path, but does not work as expected for Text Box.

For example when the TextBox is positioned at 50, 50 like this:

Canvas.SetLeft( textBox, 50.0 );
Canvas.SetTop( textBox, 50.0 );

it is drawn at that position correctly: if the text is about 20 pixels high and 50 pixels wide, the bounding rectangle on the screen is roughly L=50 T=50 R=100 B=70.

However the HitTest function returns false in that rectangle and returns true only in a rectangle L=0 T=0 R=50 B=20. In other words the hit test knows the size of the Text Box but ignores it is not positioned at 0,0.

Why does this happen, and how to work around? (I have the feeling it has something to do with the fact that for positioning the Line and Path elements I do not use SetLeft/SetTop)

  • 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-15T19:25:20+00:00Added an answer on June 15, 2026 at 7:25 pm

    Simply because the TextBox doesn’t know anything about the fact that it is positioned in a Canvas, and hence nothing about an “actual position”.

    When you call VisualTreeHelper.HitTest(textBox, p) the Point p is given in coordinates relative to the textBox visual. Is doesn’t matter where in a Canvas or any other possible container the TextBox is located.

    That you get the “right” coordinates (relative to your Canvas) for Line and Path is just because you don’t set Canvas.Left or Canvas.Top on them.

    In case you need the coordinates relative to the Canvas, you would usually do something like this:

    bool HitTest(UIElement element, Point p)
    {
        var elementPoint = new Point(
            p.X - Canvas.GetLeft(element),
            p.Y - Canvas.GetTop(element));
    
        return VisualTreeHelper.HitTest(element, elementPoint) != null;
    }
    

    or alternatively

    bool HitTest(UIElement element, Point p)
    {
        var elementPoint = canvas.TransformToDescendant(element).Transform(p);
    
        return VisualTreeHelper.HitTest(element, elementPoint) != null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have a canvas and would like to place silverlight elements within that canvas
I have a canvas where there are various amounts of child elements on the
I have a canvas that contains a couple other elements. I need this canvas
I have designed this gui in netBeans which has a canvas , a couple
I have this canvas mousedown event: $('#canvas').mousedown(function(e) { var coords = canvas.getMousePos(e); // user
I have a canvas under a div and trying to get a mouseenter event
I have a canvas where a draw some GraphNodes and add them to the
I have a Canvas, to which I've added several thousand polygons. I would like
Ok so there is a couple things to this question. First of all, i'm
I have an app that I wrote a couple years ago that uses Canvas/FBML

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.