当窗口最大化到与窗口相同的部分时调整 WPF 网格的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8602382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:13:30  来源:igfitidea点击:

resize a WPF grid when window maximized to same portions as the window

wpfgridresize

提问by Rodniko

i have a basic grid in my WPF application:

我的 WPF 应用程序中有一个基本网格:

<Grid>
    <Grid Height="260" HorizontalAlignment="Left" Margin="24,25,0,0" Name="grid1" VerticalAlignment="Top" Width="452">
        <Border BorderBrush="Red" BorderThickness="6"></Border>  
    </Grid>
</Grid>

the grid is in the middle of the window. When i maximize the window i want the grid to auto resize itself to the size of the window and stay with the same margin that i specified.

网格位于窗口的中间。当我最大化窗口时,我希望网格自动将自身调整为窗口的大小并保持我指定的相同边距。

How do i do that ? Do i have to calculate and resize the whole thing in the resize event?

我怎么做 ?我是否必须在调整大小事件中计算并调整整个事件的大小?

i get from this:

我从中得到:

enter image description here

在此处输入图片说明

to This (i dont want this):

到这个(我不想要这个):

enter image description here

在此处输入图片说明

i want the grid to resize to the same portions as it was , but for a full screen.

我希望网格调整到与原来相同的部分,但要全屏显示。

回答by snurre

Remove Width and Height.

删除宽度和高度。

<Grid>
    <Grid Margin="24,25">
        <Border BorderBrush="Red" BorderThickness="6"></Border>  
    </Grid>
</Grid>

e: Added a second grid to make it identical to OP

e:添加了第二个网格以使其与 OP 相同

回答by Samuel Slade

You could host the Gridinside of another Gridwith something like:

您可以使用以下内容托管Grid另一个内部Grid

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="25" />
        <RowDefinition />
        <RowDefinition Height="25" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="25" />
        <ColumnDefinition />
        <ColumnDefinition Width="25" />
    </Grid.ColumnDefinitions>

    <!-- Main Grid -->
    <Grid Grid.Row="1" Grid.Column="1">

        <!-- Main Content -->
    </Grid>
</Grid>

That would allow you to put all your content inside of the Main Grid, while maintaining a constant margin of 25 around all edges.

这将允许您将所有内容放在 内Main Grid,同时在所有边缘保持 25 的恒定边距。