RowDefinition Height = Auto不起作用?

时间:2014-03-19 09:17:23

标签: c# wpf rowdefinition

请查看下面的代码:

<Border Background="#242328" CornerRadius="10" Opacity="0.9">
    <Grid Width="400">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="80"/>
        </Grid.RowDefinitions>
        <TextBlock Text="{Binding [someViewModel].Title}" Margin="0,20,0,20"/>

        <StackPanel Grid.Row="1" Orientation="Vertical">
            <TextBlock Margin="20,0,0,0" Text="{Binding [someViewModel].AnotherText}"/>
        </StackPanel>

        <StackPanel Grid.Row="2" Orientation="Vertical">
            <Image Source="{Binding [someViewModel].source}" Width="100" Height="100"/>
            <TextBlock Margin="20,0,0,0" Text="{Binding [someViewModel].AnotherText2}"/>
        </StackPanel>

        <Grid Grid.Row="3" Width="250" Margin="0,10,10,10">
            <Button Width="120" Height="50" Background="#638699" Content="OK" FontSize="18"/>
        </Grid>
    </Grid>
</Border>

上面代码的输出:

enter image description here

现在我的问题很简单,为什么按钮上方有空格?我已经设置了所有RowDefinition Height="Auto",当我的变量中没有任何值时,该框是否应该缩小到只剩下按钮?

请注意所有变量,例如 [someViewModel] .Title,[someViewModel] .AnotherText,[someViewModel] .AnotherText2,[someViewModel] .Source 将为null

2 个答案:

答案 0 :(得分:4)

看看你的设计师,你会看到类似的东西

Designer

您的TextBlock和StackPanels将被渲染,如果它们具有值,则无关紧要。如果你想在没有价值时隐藏它们,你可以使用IValueConverter根据其内容确定它们的可见性。

答案 1 :(得分:0)

Auto大小的行中的某些控件的高度&gt; 0即使他们没有显示任何东西。这导致计算的行高度也> 0

例如Margin上的TextBlock会导致计算出的身高。 此外,Image控件高度设置为100,无论您是否指定了源。