VerticalAlignment=“拉伸”对于画布内的标签不起作用

发布于 2024-11-17 16:48:25 字数 649 浏览 3 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾听心声的旋律 2024-11-24 16:48:25

使用与 CanvasActualWidth 的绑定:

<Canvas>
    <Label Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Canvas}}, Path=ActualWidth}">...</Label>
</Canvas>

但如上所述,如果您对动态拉伸布局感兴趣,则 Canvas 并不是控制的理想选择。

Use a binding to the Canvas's ActualWidth:

<Canvas>
    <Label Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Canvas}}, Path=ActualWidth}">...</Label>
</Canvas>

But as mentioned above, if you are interested in dynamic stretching layouts, the Canvas is not the ideal choice of control.

治碍 2024-11-24 16:48:25

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 both Label elements to fill the Content space.

还给你自由 2024-11-24 16:48:25

假设您需要用于其他具有固定性质的对象的画布,您可以将 Canvas 覆盖在 Grid 上,然后将标签放入网格中。您可以将标签放在画布之前,使它们成为背景 z 索引(被画布对象覆盖),或将标签放在画布之后,使它们具有更高的 z 索引(将覆盖画布对象)。例如:

<Button Name="buttonCancel" Width="80" Height="40" IsCancel="True" Padding="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    <Grid>
        <Label Padding="0" FontSize="10">Esc</Label>
        <Label VerticalContentAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">Cancel</Label>
        <Canvas>
            <!-- Your Canvas content here -->
        </Canvas>
    </Grid>
</Button>

Assuming you need the canvas for other objects that are of a fixed nature, you could overlay the Canvas on a Grid, 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:

<Button Name="buttonCancel" Width="80" Height="40" IsCancel="True" Padding="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    <Grid>
        <Label Padding="0" FontSize="10">Esc</Label>
        <Label VerticalContentAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">Cancel</Label>
        <Canvas>
            <!-- Your Canvas content here -->
        </Canvas>
    </Grid>
</Button>
放赐 2024-11-24 16:48:25

重复我的评论中的解决方案,因为 (a) 你真的不需要 Canvas 并且 (b) 听起来这解决了你的问题,所以我将把它作为答案更容易被其他人看到。

Canvas 适用于固定像素大小的布局,这可能是最不常见的情况。您应该将 Canvas 替换为 Grid,如下所示,以便两个 Label 都在可用空间内动态(且独立)布局:

<Grid>
    <Label Padding="0" FontSize="10">Esc</Label>
    <Label VerticalAlignment="Center" HorizontalAlignment="Center">Cancel</Label>
</Grid>

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 your Canvas with a Grid as shown below, so that both Labels are laid out dynamically (and independently) within the available space:

<Grid>
    <Label Padding="0" FontSize="10">Esc</Label>
    <Label VerticalAlignment="Center" HorizontalAlignment="Center">Cancel</Label>
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文