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