VerticalAlignment=“拉伸”对于画布内的标签不起作用
如何在 Canvas
内使用 VerticalAlignment="Stretch"
和 Label
?我试图将按钮中的文本“取消”居中,如下面的代码所示。使用标签的固定高度和宽度并不是理想的选择。
<Button Name="buttonCancel" Width="80" Height="40" IsCancel="True" Padding="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<Canvas>
<Label Canvas.Top="0" Canvas.Left="0" Padding="0" FontSize="10">Esc</Label>
<Label VerticalContentAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">Cancel</Label>
</Canvas>
</Button>
How to use VerticalAlignment="Stretch"
with a Label
inside a Canvas
? I'm trying to center the text "Cancel" in the button as in the code below. To use fixed height and width for the label isn't a desired option.
<Button Name="buttonCancel" Width="80" Height="40" IsCancel="True" Padding="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<Canvas>
<Label Canvas.Top="0" Canvas.Left="0" Padding="0" FontSize="10">Esc</Label>
<Label VerticalContentAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">Cancel</Label>
</Canvas>
</Button>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用与
Canvas
的ActualWidth
的绑定:但如上所述,如果您对动态拉伸布局感兴趣,则
Canvas
并不是控制的理想选择。Use a binding to the
Canvas
'sActualWidth
:But as mentioned above, if you are interested in dynamic stretching layouts, the
Canvas
is not the ideal choice of control.Canvas
不会对其内容执行任何缩放布局;如果要缩放内容,在这种情况下可以使用 Grid,默认情况下,它将缩放两个Label
元素以填充Content
空间。A
Canvas
does not perform any scaling layout of its contents; if you want to scale the contents, you could use Grid in this case, which will, by default, scale bothLabel
elements to fill theContent
space.假设您需要用于其他具有固定性质的对象的画布,您可以将
Canvas
覆盖在Grid
上,然后将标签放入网格中。您可以将标签放在画布之前,使它们成为背景 z 索引(被画布对象覆盖),或将标签放在画布之后,使它们具有更高的 z 索引(将覆盖画布对象)。例如:Assuming you need the canvas for other objects that are of a fixed nature, you could overlay the
Canvas
on aGrid
, and then put the labels in the grid. You can put the labels before the canvas to make them background z-index (overwritten by canvas objects) or after the canvas to make them higher z-index (will overwrite canvas objects). For example:重复我的评论中的解决方案,因为 (a) 你真的不需要
Canvas
并且 (b) 听起来这解决了你的问题,所以我将把它作为答案更容易被其他人看到。Canvas
适用于固定像素大小的布局,这可能是最不常见的情况。您应该将Canvas
替换为Grid
,如下所示,以便两个Label
都在可用空间内动态(且独立)布局:Repeating my solution from the comments, since (a) you really don't want a
Canvas
and (b) it sounds like this solved your problems, so I'll make it an answer where it will be more visible to others.Canvas
is meant for fixed-pixel-size layouts, which is probably the least common case. You should replace yourCanvas
with aGrid
as shown below, so that bothLabel
s are laid out dynamically (and independently) within the available space: