0

Please have a look at my code below:

<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>

Output of the code above:

enter image description here

Now my question very simple, why there is empty space above the button? I've already set all RowDefinition Height="Auto", when there isn't any value in my variable, shouldn't the box shrink to left only button?

Please note that all the variable such as [someViewModel].Title, [someViewModel].AnotherText, [someViewModel].AnotherText2, [someViewModel].Source will be null

4

2 回答 2

4

看看你的设计师,你会看到这样的东西

设计师

您的 TextBlock 和 StackPanel 将被渲染,它们是否具有值并不重要。如果您想在它们没有值时隐藏它们,您可以使用 IValueConverter 根据它们的内容确定它们的可见性。

于 2014-03-19T09:27:53.717 回答
1

大小行中的某些控件Auto的高度确实 > 0,即使它们不显示任何内容。这会导致计算的行高也 > 0。

例如Margin,您的TextBlock原因是计算出的高度。此外Image,无论您是否分配源,控制高度都设置为 100。

于 2014-03-19T09:31:02.487 回答