WPF Grid&倒塌的内容

时间:2010-08-16 22:25:18

标签: .net wpf layout

我有一个基本布局的窗口:

<Window
    MinHeight="72" MaxHeight="400" Height="Auto"
    MinWidth="460" MaxWidth="460">
    <DockPanel>
        <!-- Footer -->
        <StackPanel DockPanel.Dock="Bottom">
            ...
        </StackPanel>

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="40" />
                <RowDefinition Height="Auto" MinHeight="0"/>
                <RowDefinition Height="Auto" MinHeight="0" MaxHeight="40" />
            </Grid.RowDefinitions>

            <StackPanel>
                <!-- Header -->
            </StackPanel>

            <TextBlock Grid.Row="1" Visibility="{My Fancy Binding To collapse this}" ... />

            <TextBlock Grid.Row="2" Visibility="{My Fancy Binding To collapse this}" ... />
        </Grid>
    </DockPanel>
</Window>

我想要实现的是一个紧凑的窗口,当底部的两个TextBlock是Visibility =“Collapsed”时,并且当它们不是时会扩展到最大高度设置。

这对于<Grid>来说效果还不错,但似乎无论我如何重新安排我的布局,我都无法让窗口尝试使用最小空间,它始终位于最大尺寸有很多无用的空白。

发生了什么:

+-----------------------------------+
| Header                            |
+-----------------------------------+
+-----------------------------------+
+-----------------------------------+
|                                   |
| Wasted Space I want to            |
| "collapse"                        |
|                                   |
+-----------------------------------+
| Footer                            |
+-----------------------------------+

我想要的是什么:

+-----------------------------------+
| Header                            |
+-----------------------------------+
+-----------------------------------+
+-----------------------------------+
+-----------------------------------+
| Footer                            |
+-----------------------------------+

Or Effectively:

+-----------------------------------+
| Header                            |
+-----------------------------------+
| Footer                            |
+-----------------------------------+

1 个答案:

答案 0 :(得分:2)

我认为你要找的是窗口上的SizeToContent =“Height”。