如何保持网格单元格的高度和宽度相同

编程入门 行业动态 更新时间:2024-10-10 03:29:37
本文介绍了如何保持网格单元格的高度和宽度相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在调整大小时保持 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.

更多推荐

如何保持网格单元格的高度和宽度相同

本文发布于:2023-11-23 01:04:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619547.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   宽度   单元格   高度

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!