gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:24:41 26 4
gpt4 key购买 nike

调整大小时,我需要保持Grid单元格的高度=宽度。

使用viewBox的工作代码:

  <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的想法! :)

最佳答案

执行此操作的“适当”方法可能是使用Grid控件的共享大小功能,但这可悲地阻止了扩展整个网格。例如

<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中,但是它的调整大小行为可能不是您想要的,因为它会缩放内容。也许您可以找到一种在您的环境中使其可用的方法。

关于wpf - 如何保持网格单元格的高度和宽度相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5922395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com