WPF:如何找到可用于 Canvas 的空间?
我正在使用 WPF 形状在画布上创建六边形(用于游戏地图)。在使用 ScrollViewer 一番之后,我决定自己实现地图的滚动和缩放,而不是使用 WPF 功能,只是使用 WPF 来获取鼠标滚轮、箭头键等事件。我将 (Hex Map ) Canvas 作为 Dock 面板中的最后一个子项,因此它将获得所有可用的剩余空间。 Dock 面板将被设置为主窗口的内容。但我想在将任何子项放在画布上之前找出画布有多大,以便我可以将屏幕置于我想要的六角形上,并且只添加实际可见的形状(六角形)。当缩小时,我将完全删除多边形并使用另一种渲染方法,当放大时我将添加更多细节。
有什么巧妙的方法来获取可用空间吗?我能想到的唯一可行的方法是获取窗口的当前尺寸并减去 Dock 面板外部元素的尺寸,但这感觉相当混乱。
I'm using WPF shapes to create Hexagons (for a game map) on a Canvas. After some playing around with ScrollViewer, I've decided to implement the scrolling and zoom of the map myself rather than using WPF functionality, just using WPF to get the events for mouse wheel, arrow keys etc. I'm placing the (Hex Map) Canvas as the last child inside a Dock Panel so it will get all the available remaining space. The Dock Panel will be set to be the content of the Main Window. But I want to find out how big the Canvas can be before I put any Children on the Canvas so that I can centre the screen over the Hex I want and only add the Shapes (Hexs) that can actually be seen. When zoomed out, a long way I will remove Polygons altogether and use another method of rendering and when zoomed in a long way I will add more details.
Is there any neat way of getting the available space? The only way that I can think of that will hopefully work is to get the current dimensions of the windows and subtract the dimensions of the outer elements of the Dock Panel, but that feels rather messy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
Canvas
的ActualWidth
和ActualHeight
属性来确定可用的大小。确保将HorizontalAlignment
和VerticalAlignment
设置为Stretch
。You may use the
ActualWidth
andActualHeight
properties ofCanvas
to determine size available to it. Be sure thatHorizontalAlignment
andVerticalAlignment
are set toStretch
.