【问题标题】:WPF Grid Auto Height and Fixed Height unexpected behaviour when using a RowSpan使用 RowSpan 时 WPF 网格自动高度和固定高度的意外行为
【发布时间】:2016-04-26 04:31:51
【问题描述】:

我有一些理解上的问题。我在 WPF 中有一个Grid,具有交替的固定行高和自动行高。

如果我将Label 添加到第一列中具有自动高度的行,那么这将按我的预期工作。

但是,如果我在第 1 列中添加一个带有 RowSpan 的控件,则调整大小的不是具有自动高度的行,而是调整了具有固定高度的行。

看看这个例子:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="5" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="5" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="5" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="5" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="5" />

    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="150" />
        <ColumnDefinition Width="150" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>



    <Label Grid.Row="1"
           Grid.Column="0"
           Content="Label1" />

    <Label Grid.Row="3"
           Grid.Column="0"
           Content="Label2" />
    <Label Grid.Row="5"
           Grid.Column="0"
           Content="Label3" />
    <Label Grid.Row="7"
           Grid.Column="0"
           Content="Label4" />

     <Border Grid.Row="1"
            Grid.RowSpan="7"
            Grid.Column="1"
            Height="300"
            Background="Red" />


    <!--<Label Grid.Row="2"
           Grid.Column="0"
           Content="Hallo" />-->

</Grid>

如果我在固定标签上添加标签也不合逻辑,因为我看不到这个标签,因为尺寸 5 太小了。尽管该行显示得更大。

【问题讨论】:

  • 只是不要设置Border的Height
  • 您描述的行为也不是我所预料的,似乎“自动”被保留以支持固定高度 - 奇怪!

标签: wpf grid


【解决方案1】:

我认为这是非常有趣的行为。如果您查看 GridLines 源代码,您会看到构造函数:

public GridLength(double pixels)
        : this(pixels, GridUnitType.Pixel)
    {
    }

这意味着默认情况下,RowDefenition 中的 Height 有第二个参数为 Pixel。 这就是 WPF 在您的情况下调整网格大小的原因。

有几种方法可以解决它:

  1. 如果你想削减你的控制,你应该将 MaxHeight 属性添加到 Grid 的 RowDefenitions:

        <RowDefinition Height="5" MaxHeight="5"/>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="5" MaxHeight="5"/>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="5" MaxHeight="5"/>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="5" MaxHeight="5"/>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="5" MaxHeight="5"/>
    
  2. 如果不想改变你的控件,你应该添加另一个带有 Height="*" 的行:

        <RowDefinition Height="5" MaxHeight="5"/>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="5" MaxHeight="5"/>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="5" MaxHeight="5"/>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="5" MaxHeight="5"/>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" MaxHeight="5"/>
    

并为您的行添加控件:

        <Border Grid.Row="1"
        Grid.RowSpan="8"
        Grid.Column="1"
        Height="300"
        Background="Red" />

【讨论】:

  • 好的,MaxHeigt 的解决方案有效。但这并没有削减控制。现在将调整具有自动高度的行的大小。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
  • 2012-01-01
  • 1970-01-01
相关资源
最近更新 更多