自动调整大小c#WPF窗口任何小的它离开屏幕

时间:2016-11-09 21:11:19

标签: c# wpf windows autosize

我正在尝试将WPF窗口设置为根据分辨率调整大小,但我似乎无法正确使用它。有没有人有任何我应该阅读的建议或文章来实现这个目标?

我已经提供了一些示例代码,我已尝试将其作为基础来解决问题。

此代码在1920 x 1080下可以正常工作,但任何小的代码都可以在屏幕上显示

非常感谢任何帮助,提前谢谢!

<WindowState="Maximized" WindowStyle="SingleBorderWindow">
    <Grid Margin="0,0,2,-21" RenderTransformOrigin="0.0,0.0">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Image HorizontalAlignment="Right" Height="100" Margin="0,30,-400,0" VerticalAlignment="Top" Width="145" Source="G:\Pi Project\Project Pi Logo.png" Grid.Column="1"/>
        <Label Content="The Pi Project" HorizontalAlignment="Right" Height="60" Margin="0,132,-390,0" VerticalAlignment="Top" Width="170" FontSize="16" Grid.Column="1" />
        <Button x:Name="saveProject_btn" Content=" Save&#xD;&#xA;Project" HorizontalAlignment="Right" Height="200" Margin="0,815,-415,0" VerticalAlignment="Top" Width="300" FontFamily="OCR A Extended" FontSize="36" Grid.Column="1"/>
        <Button x:Name="home_btn" Content="Return&#xD;&#xA; Home" HorizontalAlignment="Right" Height="200" Margin="0,600,-415,0" VerticalAlignment="Top" Width="300" FontFamily="OCR A Extended" FontSize="36" Click="home_btn_Click"/>
        <Label x:Name="projectCodeViewer" Content="" HorizontalAlignment="Left" Height="560" Margin="50,455,0,0" VerticalAlignment="Top" Width="1425" BorderThickness="2,2,2,2" BorderBrush="Black" FontSize="36" FontFamily="OCR A Extended" Grid.ColumnSpan="2"/>
        <ListBox x:Name="MoveCommands_LB" HorizontalAlignment="Left" Height="289" Margin="50,30,0,0" VerticalAlignment="Top" Width="648" SelectionChanged="Movement_List_Populate" Grid.ColumnSpan="2"/>
        <ListBox x:Name="Values_LB" HorizontalAlignment="Left" Height="289" Margin="735,30,0,0" VerticalAlignment="Top" Width="328"/>
        <Button Content="Add Command" HorizontalAlignment="left" Height="289" Margin="1100,30,-53,0" VerticalAlignment="Top" Width="428" FontSize="48"/>
    </Grid>

1 个答案:

答案 0 :(得分:1)

在XAML中,如果你想在你的窗口上自动定位任何类型的对象,我们非常不鼓励(在我的观点中)设置可能使你的对象超出范围的边距。

你还必须考虑你想要定位它们的位置以及它们在你调整它们时应该如何反应,例如,如果你有一个标题,它应该使用父的Width整个控制(在本例中为Window),那么它应该没有WidthHorizontalAlignment设置为Stretch而没有Margins(除非你想让它有一个空闲空间来自各方)

另一个示例是用于关闭窗口的按钮,最佳做法是将HorizontalAlignment设置为Right并设置VerticalAlignment,将其实际放置在右下角没有边距的Bottom,除了上面的内容之外,你想要让角落有一些填充。

这样当你调整大小时,一切都完美地流向相对于窗口大小的位置,保持相同的比例,这里是我上面解释的例子:

<Grid>
    <Label Content="Header" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Height="50" BorderThickness="3" BorderBrush="Black" Background="Beige"/>
    <!-- Inside this Grid we insert the Body -->
    <Grid Margin="5,55,5,60" Background="LightGray" >
        <Grid.Effect>
            <DropShadowEffect />
        </Grid.Effect>
        <Label Content="Hello World!" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="50" />
    </Grid>
    <Button Content="Exit" VerticalAlignment="Bottom" HorizontalAlignment="Right" Height="50" Width="100" Margin="0,0,5,5" />
</Grid>

我上面所做的仍然不是你如何以最好的方式解决它,因为我设置了边距而没有为Grid对象定义&#34;设置结构&#34;有一个很棒的功能,ColumnDefinitionsRowDefinitions,下面是相同的代码,但这些定义可以帮助我定义我的XAML结构中的部分:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50" />
        <RowDefinition />
        <RowDefinition Height="50" />
    </Grid.RowDefinitions>
    <Label Content="Header" Grid.Row="0" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Height="50" BorderThickness="3" BorderBrush="Black" Background="Beige"/>
    <!-- Inside this Grid we insert the Body -->
    <Grid Grid.Row="1" Margin="5,5,5,10" Background="LightGray" >
        <Grid.Effect>
            <DropShadowEffect />
        </Grid.Effect>
        <Label Content="Hello World!" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="50" />
    </Grid>
    <Button Content="Exit" Grid.Row="2" VerticalAlignment="Bottom" HorizontalAlignment="Right" Height="50" Width="100" Margin="0,0,5,5" />
</Grid>

所以你要做的就是实际结构,你可以按照我的方式制作可调整大小的布局,然后重新设置。