1

我有一个包含网格的 WPF 窗口。它的设置看起来像棋盘,所以我需要网格的正方形在调整窗口大小时保持正方形。

我尝试了很多东西,但似乎没有任何效果。我希望这只是获得正确属性的问题,而不是复杂的事情,但我需要它以任何一种方式按比例调整大小。

我也不确定这是否应该发生在网格或窗口中。我会假设 Window 因为那是调整大小的控制器,但我可能是错的。有什么建议么?

用于窗口的 xml:

<Window x:Class="TicTacToeClient.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TicTacToeClient"
    Title="Mini-Checkers V1.1" Height="600" Width="600" 
      MinHeight="200" MinWidth="200" MaxHeight="600" MaxWidth="600" >
<DockPanel>
    <Menu DockPanel.Dock="Top">
        <MenuItem Header="Game">
            <MenuItem Header="Set Ip Address" Name="IPAddressMenuItem"/>
            <MenuItem Header="Set Name" Name="SetNameMenuItem"/>
        </MenuItem>
    </Menu>
    <TextBlock DockPanel.Dock="Bottom" Text ="Status: " Name="GameStatusBar" />
    <local:TicTacToeBoard x:Name="GameBoard" />
</DockPanel>

用于网格的 xml:

<UserControl x:Class="TicTacToeClient.TicTacToeBoard"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="600" d:DesignWidth="600">

<Grid Name="MainGrid">
    <Grid.RowDefinitions>
    ....
4

3 回答 3

1

I would suggest DataBinding the Width of your Grid to the Height (or vice-versa). This will make sure that it always grows proportionally. Something as simple as this should work:

<Grid x:Name="Checkerboard" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="{Binding RelativeSource={RelativeSource Self}} Path=ActualHeight}">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>        
    <!-- Grid Contents -->
</Grid> 

And if you want your cells to all remain square, just make sure they're all * sized, so they will be spread out equally.

于 2013-04-27T05:16:32.723 回答
0

您可以尝试将您GameBoard的属性设置为并ViewBox设置为两者。像这样。它不会保持表格的纵横比统一,但会保持你的游戏板比例统一。StretchUniformStretchDirection

<Viewbox  Stretch="Uniform" StretchDirection="Both">
    <local:TicTacToeBoard x:Name="GameBoard" />
</Viewbox>
于 2013-04-27T05:48:31.863 回答
0

或者,您可以将 yor grid 放在 a ViewboxwithStretch="Uniform"

于 2013-04-27T05:45:02.333 回答