内容被裁剪然后缩放窗口

时间:2018-01-25 12:40:41

标签: c# wpf xaml scaling

enter image description here

我在Dockpanel中的网格中有一个网格,我想要适合窗口的高度。问题是内容在底部被裁剪,并且窗口越小,内容就越多。 (我在网格中有一个网格的原因是我认为内部网格是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>

1 个答案:

答案 0 :(得分:0)

感谢Sinatr给了我解决方案(见评论)。在Viewbox内部设置Dockpanel就可以了。它是一种更好的实现方式,但这段代码对我有用。

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