窗口最大化时如何使所有控件按比例调整大小? [英] How to make all controls resize accordingly proportionally when window is maximized?

查看:16
本文介绍了窗口最大化时如何使所有控件按比例调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击最大化按钮时,窗口被最大化,但控件没有按比例调整大小.使控件相应地调整大小的最佳方法是什么?我正在使用 MVVM.

When I clicked on the maximize button the window is maximized but the controls are not resized proportionally. What is the best way to make the controls resize accordingly? I am using MVVM.

这是我的代码.

<Window x:Class="DataTransfer.View.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Icon="/DataTransfer;component/View/Images/ms_msnexplore.gif"

        ResizeMode="CanResizeWithGrip"
        Title="Window1" Height="500" Width="600">
    <!--Style="{DynamicResource OfficeStyle}"-->
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!--<ResourceDictionary Source="/DataTransfer;component/View/WindowBase.xaml" />-->
                <!--<ResourceDictionary Source="/DataTransfer;component/Themes/WPFThemes/CalendarResource.xaml" />-->
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width ="*" />
    </Grid.ColumnDefinitions>
        <Button Content="Button" HorizontalAlignment="Left" Margin="52,28,0,0" VerticalAlignment="Top" Width="75" Height="22" />
        <DatePicker Name="dp" HorizontalAlignment="Left" Margin="175,25,0,0" VerticalAlignment="Top" Width="123" Text="aaa" GotFocus="DateGotFocused" LostFocus="OnLeaveArchiveDate"/>
        <Calendar HorizontalAlignment="Left" Margin="47,162,0,0" VerticalAlignment="Top"/>
        <TextBox Name="t1" HorizontalAlignment="Left" Height="23" Margin="337,23,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" LostFocus="LeaveField" />
        <RadioButton Content="RadioButton" HorizontalAlignment="Left" Margin="88,92,0,0" VerticalAlignment="Top"/>
        <CheckBox Content="CheckBox" HorizontalAlignment="Left" Margin="252,96,0,0" VerticalAlignment="Top"/>
        <ComboBox Name="combo" IsEditable="False" Text="aaa" IsReadOnly="True"
                  HorizontalAlignment="Left" Margin="337,89,0,0" VerticalAlignment="Top" Width="120" 
                  Focusable="True" GotFocus="ComboBoxGotFocused" >
            <ComboBoxItem>January</ComboBoxItem>
            <ComboBoxItem>February</ComboBoxItem>
        </ComboBox>
        <TextBlock HorizontalAlignment="Left" Height="40" Margin="260,184,0,0" TextWrapping="Wrap" Text="Text_Block" VerticalAlignment="Top" Width="257"/>

    </Grid>
</Window>

推荐答案

在 WPF 中,某些容器"控件会自动调整其内容的大小,而有些则不会.

In WPF there are certain 'container' controls that automatically resize their contents and there are some that don't.

以下是一些调整其内容大小的内容(我猜您正在使用其中的一种或多种):

Here are some that do not resize their contents (I'm guessing that you are using one or more of these):

StackPanel
WrapPanel
Canvas
TabControl

以下是一些调整其内容大小的内容:

Here are some that do resize their contents:

Grid
UniformGrid
DockPanel

因此,几乎总是最好使用 Grid 而不是 StackPanel,除非您希望自动调整大小.请注意,Grid 仍有可能调整其内部控件的大小...这完全取决于您的 Grid.RowDefinition 和 <代码>Grid.ColumnDefinition 设置:

Therefore, it is almost always preferable to use a Grid instead of a StackPanel unless you do not want automatic resizing to occur. Please note that it is still possible for a Grid to not size its inner controls... it all depends on your Grid.RowDefinition and Grid.ColumnDefinition settings:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="100" /> <!--<<< Exact Height... won't resize -->
        <RowDefinition Height="Auto" /> <!--<<< Will resize to the size of contents -->
        <RowDefinition Height="*" /> <!--<<< Will resize taking all remaining space -->
    </Grid.RowDefinitions>
</Grid>

您可以从 Grid Class 页面在 MSDN 上.您还可以从 WPF 容器控件概述 MSDN 上的页面.

You can find out more about the Grid control from the Grid Class page on MSDN. You can also find out more about these container controls from the WPF Container Controls Overview page on MSDN.

可以使用 进一步调整大小FrameworkElement.Horizo​​ntalAlignmentFrameworkElement.VerticalAlignment 属性.这些属性的默认值是 Stretch,它将拉伸元素以适应其包含控件的大小.但是,当它们设置为任何其他值时,元素将不会拉伸.

Further resizing can be achieved using the FrameworkElement.HorizontalAlignment and FrameworkElement.VerticalAlignment properties. The default value of these properties is Stretch which will stretch elements to fit the size of their containing controls. However, when they are set to any other value, the elements will not stretch.

更新>>>

回应您评论中的问题:

首先使用Grid.RowDefinitionGrid.ColumnDefinition 设置来组织一个基本结构... 添加Grid 控件是很常见的如果需要,进入外部 Grid 控件的单元格.您还可以使用 Grid.ColumnSpanGrid.RowSpan 属性使控件能够跨越 Grid 的多个列和/或行.

Use the Grid.RowDefinition and Grid.ColumnDefinition settings to organise a basic structure first... it is common to add Grid controls into the cells of outer Grid controls if need be. You can also use the Grid.ColumnSpan and Grid.RowSpan properties to enable controls to span multiple columns and/or rows of a Grid.

最常见的情况是至少有一行/列的 Height/Width"*" 这将填充所有剩余的空间,但您可以使用此设置有两个或更多,在这种情况下,剩余空间将在两个(或更多)行/列之间拆分.对于未设置为*"的行/列,自动"是一个很好的设置,但这实际上取决于您希望布局的方式.

It is most common to have at least one row/column with a Height/Width of "*" which will fill all remaining space, but you can have two or more with this setting, in which case the remaining space will be split between the two (or more) rows/columns. 'Auto' is a good setting to use for the rows/columns that are not set to '"*"', but it really depends on how you want the layout to be.

没有可以在单元格中的控件上使用的 Auto 设置,但这也一样,因为我们希望 Grid 为控件调整大小我们...因此,我们根本不想设置这些控件的 HeightWidth.

There is no Auto setting that you can use on the controls in the cells, but this is just as well, because we want the Grid to size the controls for us... therefore, we don't want to set the Height or Width of these controls at all.

我关于 FrameworkElement.Horizo​​ntalAlignmentFrameworkElement.VerticalAlignment 属性的观点只是为了让您知道它们的存在......因为它们的默认值已经Stretch,一般不需要显式设置.

The point that I made about the FrameworkElement.HorizontalAlignment and FrameworkElement.VerticalAlignment properties was just to let you know of their existence... as their default value is already Stretch, you don't generally need to set them explicitly.

Margin 属性通常只是用来均匀地间隔你的控件...如果你从 Visual Studio 工具箱中拖放控件,VS 将设置 Margin属性将您的控件准确放置在您放下它的位置,但通常,这不是我们想要的,因为它会干扰控件的自动调整大小.如果您这样做,则只需删除或编辑 Margin 属性以满足您的需要.

The Margin property is generally just used to space your controls out evenly... if you drag and drop controls from the Visual Studio Toolbox, VS will set the Margin property to place your control exactly where you dropped it but generally, this is not what we want as it will mess with the auto sizing of controls. If you do this, then just delete or edit the Margin property to suit your needs.

这篇关于窗口最大化时如何使所有控件按比例调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆