【问题标题】:Dynamically expanding Grid also expands fixed height Rows and Columns动态扩展 Grid 还扩展了固定高度的行和列
【发布时间】:2015-08-30 12:43:15
【问题描述】:

我在另一个 Grid 中有一个动态扩展的 Grid,它分为 4 行和 4 列。

我的主要内容网格跨越 4 行和 2 列并动态加载不同的视图,有时还会扩展高度。我的问题是,其他行虽然高度固定,但也被扩展了,这使得布局看起来很奇怪。

我想将所有行的高度保持为 36,但只需扩展最低行,以便显示我的网格的所有内容。

<Grid HorizontalAlignment="Stretch">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="45" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="70" />
        <ColumnDefinition Width="110" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="36" />
        <RowDefinition Height="36" />
        <RowDefinition Height="36" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Button Grid.Row="0" Grid.Column="0"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            FontSize="14"
            Content="X">
    </Button>

    <Grid x:Name="MainPaymentContentRoot"
              Grid.Row="0" 
              Grid.RowSpan="4"
              Grid.Column="1" 
              Grid.ColumnSpan="2"
              Margin="20,0,0,0"
              HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch"
              ScrollViewer.VerticalScrollBarVisibility="Auto"
              ScrollViewer.VerticalScrollMode="Auto">
    </Grid>

    <TextBlock Text="MyField" Grid.Row="0" Grid.Column="3" Margin="10,0,0,0" VerticalAlignment="Center" />
    <Button Grid.Row="1" Grid.Column="3" Margin="10,0,0,0"
            HorizontalAlignment="Stretch"
            HorizontalContentAlignment="Right"
            Content="AnotherButton">
    </Button>

</Grid>

这是针对 Win 8 开发的。

【问题讨论】:

    标签: xaml layout grid


    【解决方案1】:

    目前,您的RowDefinition 设置为Auto。这意味着该行将计算 content 大小,并相应地调整其高度。

    您需要将其更改为Height="*"

    <RowDefinition Height="*" />
    

    这将强制RowDefinition 扩展到父级 的高度。换句话说,它将占用所有可用空间。

    【讨论】:

    • 嗨,迈克,谢谢。是的,我试过了,似乎是同样的结果。我刚刚做的是将中间布局分开,使其隔离。所以本质上,我有 3 列,每列都有不同的网格。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 2019-01-27
    • 1970-01-01
    • 2014-03-29
    • 2014-05-10
    • 2020-09-30
    相关资源
    最近更新 更多