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

标签 wpf grid height width cell

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

相关文章:

c# - 检索 x :Name or x:Key from Brush

javascript - 滚动网格以显示列

c - 尝试用C语言编写单词搜索程序

height - 最小高度 Vs 高度

javascript - 响应行不考虑其中 DIV 的高度

WPF ComboBox ItemTemplate 绑定(bind)到字符串集合

c# - WPF 绑定(bind) View 作为内容

wpf - 简单的网格布局问题

HTML CSS : How to align grid to content area?

css - 100% div 高度让人头疼