简体   繁体   English

WPF:在画布上绘制网格?

[英]WPF: Draw a grid on a Canvas?

I'm currently trying to add gridlines to a canvas. 我目前正在尝试向画布添加网格线。 I need an exact free space between them, where I'd like to place something for hit detection per cell, maybe simply a transparent border or such a thing. 我需要它们之间有一个精确的自由空间,我想在每个单元格中放置一些东西来进行命中检测,也许只是透明边框或类似的东西。

While I thought this would be an easy thing, I'm facing problems like antialiasing and that lines in WPF aren't very "calculate" / exact drawing friendly - eg if I draw a line on x=20 with a thickness of 10, the line's width goes from x=15 to x=25 (maybe not exactly, just some kind like that), so it takes the given position as middle point - if it would draw from 20 to 30 it would be easier in my case. 尽管我认为这很容易,但是我遇到了诸如锯齿之类的问题,并且WPF中的线条不是非常“计算” /精确绘制友好的-例如,如果我在x = 20上绘制的线条的厚度为10,线的宽度从x = 15到x = 25(也许不完全是,只是这样),所以它将给定位置作为中间点-如果从20画到30,则对我来说更容易。 Besides that making things more complex, how does WPF handle eg a thickness of 5? 除了使事情变得更复杂之外,WPF如何处理例如5的厚度? Draw thickness 3 left from the given point and the remaining 2 right from it? 从给定点向左绘制厚度3,从给定点向右绘制厚度2? Or maybe just the opposite way? 也许正好相反? Well, just wanted to show you which problems I have, though this all maybe just seems simple to be done. 好吧,我只是想向您展示我有哪些问题,尽管看起来这一切似乎很简单。

Just wondering if anyone has ever done this before. 只是想知道以前是否有人这样做过。 Currently I find a Border without content and just 2 sites set to a thickness greater than 0 as line seems to work the best in my tests, seems like it's clear where they are drawn and they somehow don't seem to make any antialiasing problems. 目前,我发现没有内容的边框,只有2个站点的厚度设置为大于0,因为线在我的测试中似乎效果最好,似乎很清楚它们的绘制位置,而且它们似乎也不会造成任何抗锯齿问题。 Just wondering if there's a more intuitive / better way of doing this? 只是想知道是否有更直观/更好的方法来做到这一点? I don't want to lay a Canvas over a Grid, I think this maybe makes some things more complex in the end (by the way: how would I place a Canvas on top of a Grid?). 我不想在画布上放置画布,我认为这最终会使某些事情变得更加复杂(顺便说一句:我如何将画布放置在网格顶部?)。 Thanks for any hint! 感谢您的提示!

如果还有其他人遇到这个问题:我最终画了矩形;-)

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

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