如何让一个控件展开/填充到maxheight,然后在WPF中展开/填充另一个控件?

时间:2013-01-24 21:31:24

标签: wpf layout grid dockpanel

我有以下XAML源代码来演示我正在处理的内容。

我希望,当垂直调整组时,将第一个组框展开,直到其最大高度,然后,当达到该值时,展开第三个组框。第三个组框也具有最小高度属性。

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Name="Screen_1_Name"   
    x:Class="TestExpansionScreens.Screen_1"
    Width="400" Height="400">

    <Grid x:Name="LayoutRoot" Background="White" Margin="0,0,0,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <GroupBox Header="Thing1" Background="LightGreen" Grid.Row="0" Grid.Column="0" MaxHeight="350">
            <Button Content="Stuff1"  />
        </GroupBox>

        <GroupBox Header="Thing2" Background="LightBlue" Grid.Row="1" Grid.Column="0">
            <TextBox Text="Stuff2" Height="60" />
        </GroupBox>

        <GroupBox Header="Thing3" Background="Pink" Grid.Row="2" Grid.Column="0">           
            <TextBox Text="Stuff3"  />          
        </GroupBox>
    </Grid>
</UserControl>

通常,当我只想扩展一个控件以填充可用空间时,我使用DockPanel。我用各种各样的网格和dockpanels构建了这个例子,然而,我一直无法解决如何使它工作。关于如何实现它的任何想法?

由于

1 个答案:

答案 0 :(得分:0)

您必须在第一个RowDefinition上设置MaxHeight,而不是在GroupBox上设置MaxHeight。该行将长到该高度,然后所有多余的空间将被第三行占用。您还可以将MinHeight添加到第三行。

    <Grid x:Name="LayoutRoot" Background="White" Margin="0,0,0,0">
        <Grid.RowDefinitions>
            <RowDefinition MaxHeight="350" />
            <RowDefinition Height="Auto"/>
            <RowDefinition MinHeight="150" />
        </Grid.RowDefinitions>

        <GroupBox Header="Thing1" Background="LightGreen" Grid.Row="0" Grid.Column="0">
            <Button Content="Stuff1"  />
        </GroupBox>

        <GroupBox Header="Thing2" Background="LightBlue" Grid.Row="1" Grid.Column="0">
            <TextBox Text="Stuff2" Height="60" />
        </GroupBox>

        <GroupBox Header="Thing3" Background="Pink" Grid.Row="2" Grid.Column="0">
            <TextBox Text="Stuff3"  />
        </GroupBox>
    </Grid>