This is a migrated thread and some comments may be shown as answers.

FindElementsInHostCoordinate in RadWindow

2 Answers 102 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
adriel silva
Top achievements
Rank 1
adriel silva asked on 07 Oct 2010, 02:00 PM
Hi,

Vocês sabem porque o método VisualTreeHelper.FindElementsInHostCoordinate (Point, UIElement) não retorna os controles que estão dentro do RadWindow?

You know why the method VisualTreeHelper.FindElementsInHostCoordinate (Point, UIElement) does not return the controls that are inside the RadWindow?

Att,

2 Answers, 1 is accepted

Sort by
0
George
Telerik team
answered on 12 Oct 2010, 04:10 PM
Hello,

I tried to reproduce the problem, but to no avail. Could you please send us a sample running project that reproduces the problem? It would be very helpful. 

Sincerely yours,
George
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
blaise
Top achievements
Rank 1
answered on 13 Apr 2012, 11:35 PM
Hello, I had a similar problem using the radwindow.
context:
implementing a behavior to apply on a RadContextMenu (menu which appears on user right click). This behavior needs the list of the elements wich can be in the context of the mouse position, it will build menu items according to the returned elements. For instance, the first textbox found in the content tree would imply a copy past feature

naïve approach
The job is quite straight forward to implement
1. handle visual root mouse move and keep in the mouse position in a field.
void OnVisualRootMouseMove(object sender, MouseEventArgs e)
{
    MousePosition = e.GetPosition(VisualTreeRoot);
}

2. on context menu opened, loop through the elements in host coordinates, in order to build the menu items:
private void OnContextMenuOpened(object sender, RoutedEventArgs e)
{
    var elements = VisualTreeHelper.FindElementsInHostCoordinates(MousePosition, VisualTreeRoot).ToArray();
    loop elements and build some items if required...
}

what's wrong? 
This approach seems to be smart, it meets the basic requirements. However, it sucks when working with the popup contained elements: those contents do not belong to the main Visual tree. I have read some people answering that the FindElementsInHostCoordinates method could not work because of this : it's wrong. The method effectively needs an elements of the main visual tree to work. But I have read also that the method job will be good if we give any visual tree root to it : it's true. The reason why it does return nothing when sending a radwindow element is because 1- the window is not part of the main visual tree, 2- the window is not the root of it's visual tree.
hard to see, easy to fix:
Understanding this last fact, we can set our behavior more generic: let's abstract the visual tree root property. For instance, replacing the VisualTreeRoot property in the two codes above by a GetContainer() method call (see below) will make the behavior reusable: in the main visual tree, but also in a rad window visual tree; same trick could be applyed to any "out of visual tree" element.
private UIElement GetContainer()
{
    if (VisualTreeRoot is RadWindow)
        return (UIElement) VisualTreeHelper.GetParent((UIElement)VisualTreeHelper.GetParent(VisualTreeRoot));
    return VisualTreeRoot;
}




Tags
General Discussions
Asked by
adriel silva
Top achievements
Rank 1
Answers by
George
Telerik team
blaise
Top achievements
Rank 1
Share this question
or