RadContextMenu and VisualTreeHelper.FindElementsInHostCoordinates

4 posts, 0 answers
  1. GT
    GT avatar
    13 posts
    Member since:
    Oct 2009

    Posted 26 Jan 2010 Link to this post

    I'm having an issue related to RadContextMenus and the VisualTreeHelper.FindElementsInHostCoordinates method (more specifically with submenus). I understand that the RadContextMenu implementation uses the Silverlight Popup control, and that by default Popups are not added to the VisualTree and therefore child elements of the Popup CANNOT be detected using the VisualTreeHelper. If however the Popup is part of the VisualTree the VisualTreeHelper should be able to detect child elements without issue.

    So, as an attempted fix I programmatically attached the RadContextMenu's hosting Popup to the RootVisual - this worked great for all immediate menu items, however submenus and their child menuitems still could NOT be detected by the VisualTreeHelper. As it turns out submenus are actually hosted in another Popup.

    The visual heirarchy (minus irrelevant elements) is roughly:
    -Popup
       -RadContextMenu
          -RadMenuItem
             -Popup
                -RadMenuItem
                -...
          -RadMenuItem
          -...

    This also means that the submenu's Popup IS implicitly attached to the VisualTree - so the question is why doesn't this work? The best I can guess is that for some reason the Silverlight VisualTreeHelper is unable to detect Popups (and their child elements) that are hosted in other Popups regardless of whether or not they are attached to the VisualTree.

    Basically, my application requires that given an arbitrary x/y position, we can find the topmost control at that point. VisualTreeHelper.FindElementsInHostCoordinates works perfectly in all situations except for the one described above.

    This leaves me with a few questions:
    - Is there a way to ensure all RadContextMenu submenus and child menuitems can be detected using the VisualTreeHelper.FindElementsInHostCoordinates method?
    - Is there perhaps another reliable method that would fulfill my application requirements and still use the RadContextMenu?
    - Is there a reason why submenus use another Popup as opposed to a something like a Canvas?
    - Is there a reason why RadContextMenus are not automatically attached to the VisualTree in the same manner as RadWindows (via Telerik.Windows.Controls.HACKS.AttachPopupToVisualTree)?

    Thanks!
  2. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 27 Jan 2010 Link to this post

    Hi Greg,

    RadContextMenu is not added to the visual tree because it doesn't need to. In Silverlight 2 we had to do it because of bug in the framework. Also there is a chance that AttachPopupToVisualTree method won't succeed.
    We do not expose public method to iterate through popups so there is no way to detect the visual elements in some coordinates.
    SubMenuItems use popup because it is natural - you want the newly opened submenu to be on top of the other elements - including Windows.
    We may consider adding such method in future release.

    Hristo
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
  4. GT
    GT avatar
    13 posts
    Member since:
    Oct 2009

    Posted 27 Jan 2010 Link to this post

    Hi Hristo,

    Thanks for the quick response.

    I believe the issue is moreso that the Silverlight implementation of VisualTreeHelper doesn't play nicely with Popups inside of other Popups. Is it possible to specify a different Style/ControlTemplate for the RadContextMenu which would replace the submenu Popup with a control that will work with VisualTreeHelper?

    Thanks -
  5. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 28 Jan 2010 Link to this post

    Hello Greg,

    Changing how RadMenuItem works is not an easy task. Popup is essential part of RadMenuItem and I'm afraid it won't work without it.
    However I've managed to create a prototype that will take Popups in account when calling VisualTreeHelper.FindElementsInHostCoordinates.

    Take a look and let me know if this will work for you.

    Regards,
    Hristo
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top