简体   繁体   中英

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. Besides that making things more complex, how does WPF handle eg a thickness of 5? Draw thickness 3 left from the given point and the remaining 2 right from it? 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. 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!

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

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