如何保持网格单元格的高度和宽度相同 [英] How to keep grid cell height and width the same

查看:29
本文介绍了如何保持网格单元格的高度和宽度相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在调整大小时保持 Grid 单元格高度 = 宽度.

I need to keep the Grid cell height = width when resizing.

  <Viewbox>
        <Grid>
            <Grid.RowDefinitions>
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Label Grid.Row="0" Grid.Column="0" Background="Black" Width="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}"></Label>
                <Label Grid.Row="1" Grid.Column="0" Background="Gray" Width="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}"></Label>
                <Label Grid.Row="0" Grid.Column="1" Background="Gray" Width="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}"></Label>
                <Label Grid.Row="1" Grid.Column="1" Background="Black" Width="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}"></Label>
            </Grid>
        </Viewbox>

感谢 H.B.使用viewBox的想法!:)

Thank's to H.B. for the idea to use a viewBox! :)

推荐答案

执行此操作的正确"方法可能是使用 Grid 控件的共享大小功能,但这可悲地防止拉伸整个网格.例如

The "proper" way to do this is probably using the shared-size features of the Grid control, but this sadly prevents stretching the whole grid. e.g.

<Grid Grid.IsSharedSizeScope="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition SharedSizeGroup="A"/>
        <ColumnDefinition SharedSizeGroup="A"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition SharedSizeGroup="A"/>
        <RowDefinition SharedSizeGroup="A"/>
    </Grid.RowDefinitions>
    <Label Grid.Row="0" Grid.Column="0" Background="Red"   Content="Lorem"/>
    <Label Grid.Row="1" Grid.Column="0" Background="White" Content="Lorem ipsum"/>
    <Label Grid.Row="0" Grid.Column="1" Background="White" Content="Lorem ipsum dolor"/>
    <Label Grid.Row="1" Grid.Column="1" Background="Red"   Content="Lorem ipsum dolor sit"/>
</Grid>

可以将它放在 Viewbox 中,但调整大小的行为可能不是您想要的,因为它会缩放内容.也许您可以找到一种方法使其在您的上下文中可用.

One could place this in a Viewbox but the resize behavior of that is probably not what you want, since it zooms the contents. Maybe you can find a way to make this usable in your context.

这篇关于如何保持网格单元格的高度和宽度相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆