简体   繁体   中英

WPF get controls collided with line

I have a led panel which looks like this . This is built with a canvas object that contains a grid with columns and rows. Each field of that matrix contains a LED control which contains an ellipse.

Now I want to draw a line on the canvas. I think I am able to do that but how can I get all LED controls which are on that line?

Provided that you draw a System.Windows.Shapes.Line , you could get the Geometry of the Line by the RenderedGeometry property and use that Geometry as input for a geometry hit-test .

Such a hit-test would be done by a call to VisualTreeHelper.HitTest with a GeometryHitTestParameters argument. HitTest would call your HitTestResultCallback for every ellipse that is covered by the line's geometry.

I am not completely sure whether geometry hit-testing would work with "empty" geometries like a line with zero width. But you could give it a try and in case it doesn't work use for example a tilted rectangle geometry instead of the line. Or you could simulate the use of a Pen with a certain thickness by creating a "widened" geometry with Geometry.GetWidenedPathGeometry .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM