How to keep grid cell height and width the same

喜夏-厌秋 提交于 2019-12-01 02:23:56

问题


I need to keep the Grid cell height = width when resizing.

The working code using a 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>

Thank's to H.B. for the idea to use a viewBox! :)


回答1:


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>

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.




回答2:


WPF provides a UniformGrid - you might find this more helpful for what you are trying to do. Here is an article that demonstrates it: http://www.c-sharpcorner.com/UploadFile/yougerthen/308222008124636PM/3.aspx

To keep things square just bind the grid's Width property to its own ActualHeight property:

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



回答3:


I think you should use a UniformGrid instead, or you can try something like:

<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>


来源:https://stackoverflow.com/questions/5922395/how-to-keep-grid-cell-height-and-width-the-same

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!