0

我有这样的布局:

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="80" /> <!--Row 1-->
    <RowDefinition Height="*" /> <!--Row 2-->
    <RowDefinition Height="auto" /> <!--Row 3-->
  </Grid.RowDefinitions>
</Grid>

第 2 行必须是可扩展的,所以我们必须这样Height *做。

问题是,我们不希望第 3 行留在底部。换句话说,我们希望第 2 行初始化为所需的高度。

目前(第 2 行底部有许多未使用的空间):

在此处输入图像描述

希望:

在此处输入图像描述

有什么简单的方法可以得到这个吗?

4

3 回答 3

0

实际上,我自己找到了准确的解决方案,但仍然感谢@Maverik @Nitesh @Vanlalhriata 等,你们帮了我很多。

我在网格外包装了一个面板,并将其设置heightAuto,也设置VerticalAlignmentTop

这是我的完整演示解决方案,希望对您有所帮助:

<!--wrapper a new panel, set its property-->
<Grid Height="auto" VerticalAlignment="Top">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="80" />
            <RowDefinition Height="*" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0"
                    Background="Red"
                    Text="Row 1" />
        <TextBlock TextWrapping="Wrap" Grid.Row="1" Background="Gray">Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!</TextBlock>
        <TextBlock Grid.Row="2"
                    Background="Navy"
                    Text="Row 3" />
    </Grid>
</Grid>

这符合我的所有要求,例如 Microsoft Word 2013 起始页。

于 2013-08-07T10:16:49.423 回答
0

试试看

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="80" /> <!--Row 1-->
        <RowDefinition Height="*" /> <!--Row 2-->
        <RowDefinition Height="Auto" /> <!--Row 3-->            
     </Grid.RowDefinitions>

     <ContentControl Grid.Row="2" VerticalAlignment="Top"></ContentControl>
</Grid>

您必须为第 3 行的内容设置 VerticalAlignment="Top"。

于 2013-07-22T11:44:06.127 回答
0
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="80" />
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    ...
</Grid>

您需要为第 2 行设置窗口的最小高度或使用滚动查看器的最大高度,否则第 2 行可以将第 3 行推离视图。这是一个示例,显示了如何使用它:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="480">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="80" />
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Border Grid.Row="0" Background="Aquamarine">
            <TextBlock>Page Header Stuff</TextBlock>
        </Border>
        <Border Grid.Row="1" Background="DarkKhaki">
            <TextBlock TextTrimming="None" TextWrapping="Wrap">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ut gravida lorem. Ut turpis felis, pulvinar a semper sed, adipiscing id dolor. Pellentesque auctor nisi id magna consequat sagittis. Curabitur dapibus enim sit amet elit pharetra tincidunt feugiat nisl imperdiet. Ut convallis libero in urna ultrices accumsan. Donec sed odio eros. Donec viverra mi quis quam pulvinar at malesuada arcu rhoncus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In rutrum accumsan ultricies. Mauris vitae nisi at sem facilisis semper ac in est.
            </TextBlock>
        </Border>
        <Border Grid.Row="2" Background="DarkCyan">
            <TextBlock>Row 3</TextBlock>
        </Border>
        <Border Grid.Row="3" Background="DarkGray"/>
    </Grid>
</Window>
于 2013-07-22T12:14:57.190 回答