WPF - UIElement.RenderSize 不适用于线条形状
我正在尝试制作一些绘图应用程序,但在“选择模式”中得到了奇怪的结果。 如果我绘制一个矩形并“选择它”,则 RenderSize 会为其返回正确的大小,但如果选择了 Line,则 RenderSize 返回的 Size 的宽度设置为 Line.X2,高度设置为 Line.Y2。 例如:线从 X1 = 50、Y1 = 50 开始,以 X2 = 130、Y2 = 90 结束,RenderSize 返回宽度 = 130、高度 = 90 的大小。我的选择包含 UIElement 类型的元素,所以我不知道(并且真的不应该关心)选择什么形状以使我的选择模式尽可能通用,并且我想在用户移动所选形状时绘制边界框。
尝试用谷歌搜索这个问题,但没有发现任何相关的内容,所以也许你可以帮助我解决这个问题。 是因为 Rectangle 的位置是由 Canvas 设置的,而 Line 的点是明确设置的吗?
I'm trying to make some drawing application and I get strange results in my "selection mode". If I draw a rectangle and "select it" RenderSize returns proper Size for it, but if Line is selected RenderSize returns Size which has Width set as Line.X2, and Height set as Line.Y2. For example: Line begins at X1 = 50, Y1 = 50, ends at X2 = 130, Y2 = 90, RenderSize returns Size with Width = 130 and Height = 90. My selection contains elements of type UIElement so I don't know (and really shouldn't care) what shape is selected in order to make my selection mode as generic as I can and I'd like to draw bounding box while user moves selected shape.
Tried google the problem but found nothing relevant so maybe you could help me with it. Is it because Rectangle has position set by Canvas while Line has its points set explicitly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您获得 130x90 的原因是您引用的原因。 WPF 中的矩形是无位置的,它只是一个高度/宽度,因此两个大小值相等。
然而,由点定义的线必须定义距原点所需的偏移量,因此该偏移量包含在边界框中。
另请注意,您可以继续使用 Line 对象的 Canvas.Top/Left 属性来进一步偏移它,例如:
The reason you're getting 130x90 is because of the reason you cited. A Rectangle in WPF is position-less, it's just a height/width so the two size values are equal.
However a Line as defined by points necessarily defines a required offset from the origin, and thus the offset is included in the bounding box.
Also note that you can continue to use the Canvas.Top/Left properties with your Line object to further offset it, e.g.: