views:

27

answers:

2

In my Canvas, I have an image object. I haven't set a source in it, but it has coordinates and a size. The tag is:

<Image Canvas.Top="50" Canvas.Left="20" Height="68" Width="110" HorizontalAlignment="Left" Name="image1" Stretch="Fill" VerticalAlignment="Top" />

Here's the problem: When I move the mouse over it, I want to find it. With this code:

VisualTreeHelper.FindElementsInHostCoordinates(point, Application.Current.RootVisual)

It won't find the image unless a source is set. If no source is set, then the image isn't returned. Does anyone know why? This is causing me problems. I have some drag/drop code and I'm looking to drop something on that Image control, but I need to know when the mouse is over it.

I know there are other ways I could do it (such as placing something else in that location, like a grid or something and detecting that), but that's not going to work. I can think of several ways that will work, but they're far less elegant.

If I could get the above to return my image, that would definitely be ideal.

A: 

In your code you had Point point = e.GetPosition(_canvas); This gets the MousePosition co-ordinates in relation to the canvas, but your second line VisualHelper.FindElementsInHostCoordinates(point,Application.Current.RootVisual) is searching in relation to the whole page. You need to change one or the other so they in relation to the same control. I would just change to VisualHelper.FindElementsInHostCoordinates(point,_canvas)

Stephan
I was so excited because this seemed like such an obvious solution. But alas, it wasn't. Setting it to true had no effect. Further examination revealed it was set to true by default.Thanks, though.
Pete
Where are you getting `point` from? Is that from a MouseEvent on the Image itself? The other thing to check is to make sure that the coordinates of point are in relation to `Application.RootVisual` since that is your reference object.
Stephan
This is for drag/drop code. The code in question is: Point point = e.GetPosition(_canvas); foreach(UIElement element in VisualTreeHelper.FindElementsInHostCoordinates(point, Application.Current.RootVisual))Everything at that point (including a grid that I use to give a background) shows up. I see the image that I'm dragging, the grid, canvas, and user control at that point. I tried removing the grid on the chance it was causing the image at that location to not show appear, but all that changed as the the grid no longer showed up in the list of elements.
Pete
That's your problem. Will update to explain.
Stephan
Actually, that's still not it. The Canvas is 100% of the user control (rootvisual). In my Xaml I have: <Grid Canvas.Top="50" Canvas.Left="20" Height="68" Width="110" Background="White" ></Grid> <Image Canvas.Top="50" Canvas.Left="20" Height="68" Width="110" HorizontalAlignment="Left" Name="image1" Stretch="Fill" VerticalAlignment="Top" custom:DragInUserControl.DropTarget="true" />Notice the grid has exactly the same coordinates as the associated image. The grid is returned by FindElementsInHostCoordinates, but the Image is not.
Pete
And I changed the FindElementsInHostCoordinates to use _canvas and it's still not working.
Pete
I solved it by adding my own hit test code. Instead of relying on FindElementsInHostCoordinates, I used GetChildrenCount() and GetChild() to recursively go through the tree and create a Rect based on the Canvas Top and Left and the UIElement's height and width. This worked perfectly, as the Image object is there with the proper coordinates. Unfortunately reflector is useless for figuring out why FindElementsInHostCoordinates doesn't work because the code that's not finding it is unmanaged.
Pete
Glad you found a workaround. It's not a surprise that the code is unmanaged as a lot of Silverlight is.
Stephan
A: 

You can add a <Border> object around the Image object.

Or you can add eventhandlers for the mouseenter and mouse leave and change the Cursor.

rdkleine