Viewbox weirdly stretches (but only in one axis) wpf

25 Views Asked by At

I'm creating Snake game, in which I can change amount of rows and columns.

  • When I set for example 3 cols and 30 rows, everything works fine, game Grid fits in Viewbox (which is a square), overlays are above grid, working fine.
  • However, if I switch parameters to 30 columns and 3 rows, everything breaks, because Viewbox containing all of it stretches and width is much greater than height. Overlays are very small.

I want it to behavior the same way in horizontal as it do in vertical axis. I want to just make that Grid inside viewbox smaller on screen, instead of making viewbox wider.

Expected behavior (30row,3col):

Expected behavior

Unexpected behavior (3row,30col):

Unexpected behavior

<Window 
[...]
x:Name="MainGameWindow"
Height="500" 
Width="800"
>
    <Viewbox x:Name="MainWindowViewbox">
        <Grid x:Name="MainGrid" 
        VerticalAlignment="Center" 
        HorizontalAlignment="Center">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="171*" />
                <RowDefinition Height="232*"/>
            </Grid.RowDefinitions>
    [...]
    <!-- Border containing GameGrid in which I can set rows/columns-->
    <Border x:Name="GridBorder"
        Grid.Row="1"
        HorizontalAlignment="Center"
        VerticalAlignment="Top"
        RenderOptions.EdgeMode="Aliased" Grid.RowSpan="2">

        <UniformGrid x:Name="GameGrid"
            Width="400"
            Height="400"
            SnapsToDevicePixels="True"
            RenderOptions.BitmapScalingMode="HighQuality"
        />
    </Border>
[...]
</Grid>
</Viewbox>
</Window>

(full code on github)

I've tried:

  • setting width,height on viewbox to same value, but it stretched anyway
  • setting Stretch on viewbox to different values
  • setting StretchDirection
  • setting horizontal/vertical alignment on viewbow/grid
  • combining all of those and asking AI, didn't help
  • setting width and height to window containing it, forcing it to be a square
0

There are 0 best solutions below