views:

129

answers:

2
+1  Q: 

HitTest property

Hi,

I'm new to silverlight and trying to read a silverlight tutorial that uses HitTest method to know when the mouse is over a control. But unfortunately i cant see any method with this name.

Where is the HitTest method? is that because i'm using silverlight 4? is there any replacement method ?

A: 

I think you are looking for the MouseEnter and MouseLeave events.

http://msdn.microsoft.com/en-us/library/system.windows.uielement.mouseenter%28VS.95%29.aspx

You can subscribe to those events and set a flag which indicates that the mouse is over your element, or do whatever else your application needs.

jkohlhepp
i'm building a demo application that you can drag and drop a control wherever you want in the form except on a circule control(exist in the form).In the tutorial they use circule.HitTest(e.GetPosition(null)) where e is the "MouseEventArgs".What happened to this method(hitTest) why i cant use it?
Gaby
+2  A: 

Older versions (pre 3.0) did have a HitTest method. In Silverlight 3 and 4 you ouwl use the VisualTreeHelper.FindElementsInHostCoordinates method to acheive a similar goal.

For example the following code could be used in a mouse event on surface over which you might be dragging an item. It will determine if any part of the dragged item overlaps the target item. Warning air code

var container = (UIElement)sender;
var transform = draggedItem.TransformToVisual(container);

Rect rect = new Rect(transform.Transform(new Point(0, 0)), 
   new Size(draggedItem.ActualWidth, draggedItem.ActualHeight);

bool hit = VisualTreeHelper.FindElementsInHostCoordinates(rect, container)
  .Any(elem => elem == targetItem);
AnthonyWJones