dingoglotz
dingoglotz

Reputation: 2833

WPF Canvas Is rectangle at position

How can I check if a rectangle object is at a specific position in a canvas? The only solution I found is going through all children in the canvas and check if their position matches, but I don't think that is a good solution.

So I need something like Canvas.IsRectangleAtPosition(Point x), is there anything like that?

If not, how can I implement such a method?

Kind regards

Upvotes: 1

Views: 1161

Answers (1)

Clemens
Clemens

Reputation: 128106

In order to find the top-most UI element (the one that would get mouse input) you could simply call InputHitTest.

Point pos = ...
Rectangle rect = canvas.InputHitTest(pos) as Rectangle;

You could also use VisualTreeHelper.HitTest.

Rectangle rect = VisualTreeHelper.HitTest(canvas, pos).VisualHit as Rectangle;

Upvotes: 2

Related Questions