完美的中心在WPF帆布 [英] Perfect Center on WPF Canvas
问题描述
由于画布需要一个Top / Left放置,如果你想要中心的东西,正在添加一个网格在正确的Canvas.Top与HorizontalAlignment =中心的最好的方法,或有一个更好的方法?
Since the canvas requires a Top/Left for placement, if you want to center something, is adding a grid at the proper Canvas.Top with HorizontalAlignment="Center" the best way to do it, or is there a better way?
这个剪辑是一个150X300的画布,有一些内容集中在网格....
This snip is a 150X300 canvas, with some content centered in a grid ....
<Canvas Width="150" Height="300">
<Grid Canvas.Top="75" Width="106" HorizontalAlignment="Center">
{whatever you want centered}
</Grid>
</Canvas>
推荐答案
Guy的解决方案可行,但您可能需要调整z
Guy's solution works, but you may have to tweak z-order and visibility if you're juggling hit testing.
另一个替代方法是在Canvas(如你在XAML中指定的)中使用Grid,使用Height /宽度设置为(或绑定到)画布的高度/宽度。然后将HorizontalAlignment / VerticalAlignment设置为网格内容的中心。
Another alternative is having the Grid inside the Canvas (as you've specified in your XAML) with the Height/Width set to (or bound to) the Height/Width of the Canvas. Then setting HorizontalAlignment/VerticalAlignment to Center for the contents of your Grid.
这篇关于完美的中心在WPF帆布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!