locked
Why does VisualTreeHelper.FindElementsInHostCoordinates() return null? RRS feed

  • Question

  • Here is the my code for debugging this issue:

    List<UIElement> list = VisualTreeHelper.FindElementsInHostCoordinates(new Point(100, 100), LayoutRoot as UIElement) as List<UIElement>;
     

    list is always null. LayoutRoot is the root element that is filled with children.  I thought this call would return at least one element.  Could anyone kindly point out what I am missing?  


    Hong

    Saturday, December 7, 2013 7:18 PM

Answers

  • The problem isn't in the FindElementsInHostCoordinates call but in the cast. FEIHC returns an IEnumerable<UIElement>, not a List<UIElement>, so the "as List<UIElement>" conversion fails and returns null.

    Change the declaration to a var or to IEnumerable<UIElement> and your breakpoint should get hit.

    --Rob

    • Marked as answer by Hong (MA, USA) Saturday, December 7, 2013 11:34 PM
    Saturday, December 7, 2013 11:26 PM
    Moderator

All replies

  • Do you have a child element at point 100,100? FindElementsInHostCoordinates(Point) will find only elements at that specific point.

    --Rob

    Saturday, December 7, 2013 10:06 PM
    Moderator
  • Rob, the page is full of elements.  After reading your response, I tested the following code:

    for (int iX = 0; iX < 1000; iX++)
    {
        for (int iY = 0; iY < 1000; iY++)
        {
            List<UIElement> list = VisualTreeHelper.FindElementsInHostCoordinates(new Point(iX, iY), LayoutRoot as UIElement) as List<UIElement>;
            if (list != null)
            {
                Debug.WriteLine("Bingo!");
            }
        }             
    }
    I did not get "Bingo". I must be missing something fundamental.


    Hong

    Saturday, December 7, 2013 10:38 PM
  • The problem isn't in the FindElementsInHostCoordinates call but in the cast. FEIHC returns an IEnumerable<UIElement>, not a List<UIElement>, so the "as List<UIElement>" conversion fails and returns null.

    Change the declaration to a var or to IEnumerable<UIElement> and your breakpoint should get hit.

    --Rob

    • Marked as answer by Hong (MA, USA) Saturday, December 7, 2013 11:34 PM
    Saturday, December 7, 2013 11:26 PM
    Moderator
  • Thank you, Rob. That crossed my mind, but I did not bother to test it before reading your answer.

    I ported the code from a Windows Phone 8 app.  Such casting in WP8 apparently is not a problem, and this is why I did not have much doubt about it.


    Hong

    Saturday, December 7, 2013 11:38 PM
  • You got lucky on WP8: the call will succeed if the original IEnumerable is a List, but there's no guarantee of that. In the WP8 case the implementation happened to use one and so your code worked. This isn't contractual though, and in theory it could change with any update.

    I would fix this in the WP8 app as well the next time you do an update.

    --Rob

    Sunday, December 8, 2013 12:47 AM
    Moderator
  • Thank you, Rob.  I will fix the WP8 app per your suggestion. It is a good lesson.

    Hong

    Sunday, December 8, 2013 12:55 AM