简体   繁体   English

WPF获取控件与行冲突

[英]WPF get controls collided with line

I have a led panel which looks like this . 我有一个看起来像这样的led面板。 This is built with a canvas object that contains a grid with columns and rows. 这是使用canvas对象构建的,canvas对象包含具有列和行的网格。 Each field of that matrix contains a LED control which contains an ellipse. 该矩阵的每个字段都包含一个包含椭圆的LED控件。

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? 我想我能够做到这一点,但是如何获得该行上的所有LED控件?

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 . 如果您绘制了System.Windows.Shapes.Line ,则可以通过RenderedGeometry属性获取Line的Geometry并将该Geometry用作几何命中测试的输入。

Such a hit-test would be done by a call to VisualTreeHelper.HitTest with a GeometryHitTestParameters argument. 这样的命中测试将通过使用GeometryHitTestParameters参数调用VisualTreeHelper.HitTest来完成。 HitTest would call your HitTestResultCallback for every ellipse that is covered by the line's geometry. HitTest会针对线的几何覆盖的每个椭圆调用HitTestResultCallback

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 . 或者,您可以通过使用Geometry.GetWidenedPathGeometry创建“加宽”的几何图形来模拟具有一定厚度的Pen的使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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