首页 > 解决方案 > WPF: how to set window height equal to Grid height

问题描述

Using WPF, I have a Window, that is filled with one Grid. Currently this Grid has one Row and three Columns, each cell filled with the same UserControl. Future versions might add more Rows to the Grid.

I want to make my window have the size of the Grid.

<Window
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns: ...
    ...
    mc:Ignorable="d"
    d:DesignHeight="600" d:DesignWidth="600"
    Title="MainWindow" Height="600" Width="600">

<Grid Name="MainGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <View:ImageView Grid.Row="0" Grid.Column="0" DataContext="..."/>
    <View:ImageView Grid.Row="0" Grid.Column="1" DataContext="..."/>
    <View:ImageView Grid.Row="0" Grid.Column="2" DataContext="..."/>
</Grid>

The Window has a Width of 600. The Grid will stretch to this window. It will have a Width about 600. The Grid will have three columns with the same Width, something near 200. The ImageView in each cell will stretch to fill the Cell. Each Cell will have a Width about 200.

This determines the Height of the Cells, and thus the Height of the Grid.

Addition: after comments I don't want to resize the window. After it decided about what it should displays and what sizes the controls should have to display it nicely, there is no need to resize it.

For debugging, to see what happens, I resized the mainwindow, and recorded what happens to the various Height / Width / ActualHeight / ActualWidth values.

I noticed that the Grid is resized, such that its Width exactly fills the Width of the Window. Resizing will also Resize the cells and the ImageViews that are in the Cells.

This fitting is not the case with the Height. I can make the Height window Higher and Lower than the Height of the Grid.

I want to set the Height of the window such that it is exactly around the Grid.

I expected something like:

<Window ... Width = "600" Height = "auto" />

Nope, the height is way large than it should be, somewhere near 1000. The Grid is still about 300 x 600.

<Window ... Width = "600" Height = "*" /> 

This leads to an exception.

Maybe Binding? Something like this?

<Window ... Width = "600" Height = "{Binding Height, ElementName=MainGrid, Mode=OneWay}"/>
<Window ... Width = "600" Height = "{Binding ActualHeight, ElementName=MainGrid, Mode=OneWay}"/>

Nope, still way to large.

So, how to make sure that the window is exactly around the Grid?

标签: c#wpfxaml

解决方案


Height从窗口中删除 并设置SizeToContentHeight。但是,这只会设置您最初期望的宽度和高度,而不是调整大小。

<Window
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns: ...
    ...
    mc:Ignorable="d"
    d:DesignHeight="600"
    d:DesignWidth="600"
    Title="MainWindow"
    Width="600"
    SizeToContent="Height">

如果您想像您描述的那样调整窗口大小,这意味着要保持纵横比,这在没有意想不到的效果的情况下不容易解决,或者您必须将调整大小限制在水平方向,这也不是微不足道的。你可以看看这些相关的问题。

当然,您也可以监听窗口的调整大小事件并在那里进行自定义处理,但是这种方法以及上述方法可能会损害您的用户体验并可能引入不需要的效果,因此重新审视布局概念可能更明智。


推荐阅读