内容被裁剪然后缩放 window

Content gets cropped then scaling window

我在 Dockpanel 中的网格中有一个网格,我希望它适合 window 的高度。问题是内容在底部被裁剪,window 越小裁剪越多。 (我在网格中有一个网格的原因是我希望内部网格为 8*8 而不管我对 gui 做了什么)

<Window x:Class="WPF_test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPF_test"
    mc:Ignorable="d"
    Title="MainWindow" WindowState="Maximized">
<DockPanel HorizontalAlignment="Center" VerticalAlignment="Stretch" Height="Auto" Width="Auto" Margin="0,0,0,0">
    <Menu Width="200" Height="40" VerticalAlignment="Top"/>
    <Grid VerticalAlignment="Stretch" 
          Height="{Binding ActualHeight, 
          RelativeSource ={RelativeSource AncestorType = {x:Type Window}}}" 
          Width="{Binding ActualHeight, 
          RelativeSource ={RelativeSource AncestorType = {x:Type Window}}}" 
          Margin="0,0,0,0" ShowGridLines="True">
        <Grid.Background>
            <ImageBrush Stretch="UniformToFill" 
                ImageSource="C:/Users/ppeterss/OneDrive/Pictures/sjakk/red_mahogany_wood_texture_by_sweetsoulsister.jpg" />
        </Grid.Background>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
        <Grid Height="Auto" Width="Auto" Margin="0,0,0,0" ShowGridLines="True" 
              Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="8" Grid.RowSpan="8" >
            <Grid.Background>
                <ImageBrush Stretch="Fill"
                ImageSource="C:/Users/ppeterss/OneDrive/Pictures/sjakk/WhiteBoards.png" />
            </Grid.Background>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="1*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
        </Grid>
    </Grid>
</DockPanel>

感谢 Sinatr 给我解决方案(见评论)。将 Dockpanel 放在 Viewbox 中就可以了。这可能是实现它的更好方法,但这段代码对我有用。

<Window x:Class="WPF_test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPF_test"
    mc:Ignorable="d"
    Title="MainWindow" WindowState="Maximized">
<Viewbox>
    <DockPanel HorizontalAlignment="Center" VerticalAlignment="Stretch" Height="Auto" Width="Auto" Margin="0,0,0,0">
        <Menu Width="200" Height="40" VerticalAlignment="Top"/>
        <Grid VerticalAlignment="Stretch" 
          Height="{Binding ActualHeight, 
          RelativeSource ={RelativeSource AncestorType = {x:Type Window}}}" 
          Width="{Binding ActualHeight, 
          RelativeSource ={RelativeSource AncestorType = {x:Type Window}}}" 
          Margin="0,0,0,0" ShowGridLines="True">
            <Grid.Background>
                <ImageBrush Stretch="UniformToFill" 
                ImageSource="C:/Users/ppeterss/OneDrive/Pictures/sjakk/red_mahogany_wood_texture_by_sweetsoulsister.jpg" />
            </Grid.Background>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="2*"/>
                <ColumnDefinition Width="2*"/>
                <ColumnDefinition Width="2*"/>
                <ColumnDefinition Width="2*"/>
                <ColumnDefinition Width="2*"/>
                <ColumnDefinition Width="2*"/>
                <ColumnDefinition Width="2*"/>
                <ColumnDefinition Width="2*"/>
                <ColumnDefinition Width="1*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="2*"/>
                <RowDefinition Height="2*"/>
                <RowDefinition Height="2*"/>
                <RowDefinition Height="2*"/>
                <RowDefinition Height="2*"/>
                <RowDefinition Height="2*"/>
                <RowDefinition Height="2*"/>
                <RowDefinition Height="2*"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            <UniformGrid Name="Board" Columns="8" Rows="8" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="8" Grid.RowSpan="8">
                <UniformGrid.Background>
                    <ImageBrush Stretch="Fill"
                ImageSource="C:/Users/ppeterss/OneDrive/Pictures/sjakk/WhiteBoards.png" />
                </UniformGrid.Background>
            </UniformGrid>
        </Grid>
    </DockPanel>
</Viewbox>