Gaby
Gaby

Reputation: 3033

HitTest property

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 ?

Upvotes: 2

Views: 1158

Answers (2)

AnthonyWJones
AnthonyWJones

Reputation: 189525

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);

Upvotes: 3

RationalGeek
RationalGeek

Reputation: 9609

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.

Upvotes: 0

Related Questions