行设置为*的网格布局

时间:2013-07-22 11:39:32

标签: wpf wpf-controls

我有这样的布局:

<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行底部有许多未使用的空格):

enter image description here

祝:

enter image description here

有没有简单的方法来获得这个?

3 个答案:

答案 0 :(得分: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”。

答案 1 :(得分: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>

答案 2 :(得分:0)

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

我在网格外部包装面板,并将其height设置为Auto,同时将VerticalAlignment设置为Top

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

<!--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起始页。