【问题标题】:How to keep grid cell height and width the same如何保持网格单元格的高度和宽度相同
【发布时间】:2011-08-20 19:30:22
【问题描述】:

我需要在调整大小时保持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的想法! :)

【问题讨论】:

  • 看起来你错过了代码!
  • 您应该单击 Matt West 答案旁边的复选标记大纲以接受它,因为它回答了您的原始问题。另外请避免在一个问题中提出多个问题或将您的原始问题转换为完全不同的问题。如果您在裁剪搜索方面遇到问题,并且如果没有什么可以帮助就这个问题提出一个新问题。 (当然,除非修复剪辑本身被认为是一种 hack,并且您实际上想要一个更好的解决方案,这也意味着您的问题实际上没有得到充分回答)

标签: wpf grid height width cell


【解决方案1】:

执行此操作的“正确”方法可能是使用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 中,但它的大小调整行为可能不是您想要的,因为它会缩放内容。也许您可以找到一种方法使其在您的上下文中可用。

【讨论】:

    【解决方案2】:

    WPF 提供了UniformGrid - 您可能会发现这对您正在尝试做的事情更有帮助。这是一篇演示它的文章: http://www.c-sharpcorner.com/UploadFile/yougerthen/308222008124636PM/3.aspx

    为了保持正方形,只需将网格的 Width 属性绑定到它自己的 ActualHeight 属性:

    Width="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}"
    

    【讨论】:

    • 除了 UniformGrid 在调整大小时不会将其子项保持为正方形。
    • 已更新以满足此要求
    • hmm,这将使孩子保持正方形(假设您没有设置ColumnsRows),但如果高度大于网格的宽度,它将无法正确缩放.
    【解决方案3】:

    我认为您应该改用UniformGrid,或者您可以尝试以下方法:

    <Grid ShowGridLines="True" x:Name="grid" >
      <Grid.RowDefinitions>
        <RowDefinition Height="100" />
        <RowDefinition Height="50" />
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{Binding ElementName=grid, Path=RowDefinitions[0].Height}" />
        <ColumnDefinition Width="{Binding ElementName=grid, Path=RowDefinitions[1].Height}" />
      </Grid.ColumnDefinitions>
    </Grid>
    

    【讨论】:

    • 请注意:一般情况下,如果您同意其他人已经给出的答案,您应该直接提高他们的答案,而不是重复。
    猜你喜欢
    • 1970-01-01
    • 2019-06-21
    • 2014-01-19
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 2022-10-16
    相关资源
    最近更新 更多