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 5985095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:28:43+00:00 2026-05-22T22:28:43+00:00

I am trying to do hit testing on a collection of user controls added

  • 0

I am trying to do hit testing on a collection of user controls added to a canvas at runtime.

My canvas:

<Canvas x:Name="LayoutRoot" Background="White">
    <Canvas  x:Name="Carrier" Canvas.ZIndex="-1">
    </Canvas>
</Canvas>

My canvas code:

    public MainPage()
    {
        InitializeComponent();

        var uiElement = new MyUserControl();
        this.Carrier.Children.Add(uiElement);

        MouseLeftButtonDown += MouseLeftButtonDownHandler;
    }

    private List<UIElement> HitSprite(Point p)
    {
        var hitElements = 
           VisualTreeHelper.FindElementsInHostCoordinates(p, this.Carrier) as List<UIElement>;

        return hitElements.Count > 0 ? hitElements : null;
    }

    private void MouseLeftButtonDownHandler(object sender, MouseButtonEventArgs e)
    {
        var list = HitSprite(e.GetPosition(this.LayoutRoot));
    }

My user control:

<Canvas x:Name="Container" AllowDrop="True">
    <StackPanel Name="Description" Height="65" Canvas.ZIndex="2" IsHitTestVisible="False">
        <TextBlock Text="Testing" x:Name="Name" FontSize="12" FontWeight="Bold" HorizontalAlignment="Center" IsHitTestVisible="False"/>

        <Border Background="Green">
            <Image x:Name="Body" Stretch="None" Canvas.ZIndex="0" Height="20" Width="20">
            </Image>
        </Border>
    </StackPanel>
</Canvas>

I’ve tried various combinations of Carrier and LayoutRoot for GetPoint and FindElementsInHostCoordinates but the elements returned are always empty.

What am I doing wrong?

  • 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-05-22T22:28:44+00:00Added an answer on May 22, 2026 at 10:28 pm

    There are a couple of things you have to be aware of about hit testing:

    1. Elements without a background won’t appear in the hit test, so set the background, even setting it to transparent work. This happens because objects need to be “solid” for hit testing.
    2. Elements without a width and height won’t appear in the hit test, because they do not have a body to hit.
    3. Elements where IsHitTestVisible=false won’t appear in the hit test, they have been explicitly excluded from it.

    In your case, a combination of this factors was making your user control invisible to hit test:

    1. Your user control is not solid, it does not have a background.
    2. The canvas that serves as layout root in your user control does not have a size set, and therefore width and height are zero. It does not have a background either.
    3. All children elements in your user control have IsHitTestVisible=false, which exclude them from the hit test explicitly.

    I made a couple of modifications to your user control XAML and the code behind and I see your user control in the hit test result now:

        public MainPage()
        {
            // Required to initialize variables
            InitializeComponent();
    
            var uiElement = new MyUserControl();
            this.Carrier.Children.Add(uiElement);
    
            MouseLeftButtonDown += MouseLeftButtonDownHandler;
        }
        private List<UIElement> HitSprite(Point p)
        {
            var hitElements =
               VisualTreeHelper.FindElementsInHostCoordinates(p, this.LayoutRoot) as List<UIElement>;
    
            return hitElements.Count > 0 ? hitElements : null;
        }
    
        private void MouseLeftButtonDownHandler(object sender, MouseButtonEventArgs e)
        {
            var list = HitSprite(e.GetPosition(this.LayoutRoot));
        }
    

    XAML:

    <Grid x:Name="Container" AllowDrop="True" Background="Transparent">
        <StackPanel Name="Description" Height="65" Canvas.ZIndex="2" IsHitTestVisible="False">
            <TextBlock Text="Testing" x:Name="Name" FontSize="12" FontWeight="Bold" HorizontalAlignment="Center" IsHitTestVisible="False"/>
    
            <Border Background="Green">
                <Image x:Name="Body" Stretch="None" Canvas.ZIndex="0" Height="20" Width="20">
                </Image>
            </Border>
        </StackPanel>
    </Grid>
    

    As an additional advice, it is not a good idea to nest canvas panels unless there is a specific reason to do so. Canvas is a special panel in many ways, for example, it won’t clip the items that go outside its bounds, it does not have a size by default unless you set one and it is not a solid object unless you set a background explicitly. Try to use Grid and StackPanel to arrange objects and use the canvas for specific positioning on (x,y) coordinates only.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a problem I hit when trying to implement a game using the
I hit this error while my web application was trying to execute a SELECT
I'm trying to teach Komodo to fire up IDLE when I hit the right
I am trying to learn ASP.NET MVC and I hit this problem: I have
I am trying to setup a php page which uses cURL to hit a
I'm trying to do a cross domain POST request and have hit a wall
I'm trying to extend the SPL ArrayObject but I've hit a little snag. Using
I have a site I'm trying to build and I've hit one little snag
I'm trying to write a wrapper for Winamp input plugins and have hit a
I am trying to learn how Python reloads modules, but have hit a roadblock.

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.