【问题标题】:Extra space under bottom positioned TextBlock (Windows Phone 8)底部定位的 TextBlock 下的额外空间 (Windows Phone 8)
【发布时间】:2015-12-12 20:00:29
【问题描述】:

我在将 TextBlock 元素定位在新闻源矩形底部时遇到问题。 我附上了问题的图片(红色箭头指向它)。 “timeago”TextBlock 底部的多余空间过多。

您可以看到,仅当电影标题短到足以放入第一行时才会出现问题。 我猜这会使 StackPanel 缩小,因此随着时间流逝而移动 TextBlock。

我尝试将父 StackPanel 的 MinHeight 更改为较低的值,甚至删除该属性,但它似乎不起作用。 我猜问题出在包含内容星星(不可见)和“timeago”的“内部”StackPanel

这是我的 LongListSelector 的 DataTemplate

<DataTemplate>
    <Grid Margin="12,2,5,4" >
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Rectangle Fill="{StaticResource PhoneAccentBrush}"
                           Opacity="0.75"
                           Margin="0,0"
                           HorizontalAlignment="Stretch"
                           VerticalAlignment="Stretch"/>

        <StackPanel Grid.Row="0" Orientation="Horizontal" 
                        Background="Transparent" 
                        Height="auto"
                        MinHeight="99"
                        Width="auto">

            <Grid HorizontalAlignment="Center" 
                          Background="#FFFFC700" 
                          Height="auto" 
                          Width="99">
                <Image Source="{Binding ImageUrl}" 
                       Height="auto" 
                       Width="auto" 
                       Stretch="UniformToFill" 
                       Margin="0" 
                       HorizontalAlignment="Center" 
                       VerticalAlignment="Stretch">
                </Image>
            </Grid>


            <StackPanel Width="311" Margin="8,-7,0,0">
                    <TextBlock Margin="10,15,15,0" Text="{Binding Content}" Style="{StaticResource NewsFeedHighlitedContent}" FontSize="24" />

                <Grid Margin="10,5,0,15" Visibility="{Binding StarsVisibility}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="30" />
                        <ColumnDefinition Width="30" />
                        <ColumnDefinition Width="30" />
                        <ColumnDefinition Width="30" />
                        <ColumnDefinition Width="30" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                    </Grid.RowDefinitions>

                    <Image Source="{Binding StarOne}" Height="30" Margin="0,0" Width="30" Stretch="UniformToFill" Grid.Row="0" Grid.Column="0" />
                    <Image Source="{Binding StarTwo}" Height="30" Width="30" Stretch="UniformToFill" Grid.Row="0" Grid.Column="1" />
                    <Image Source="{Binding StarThree}" Height="30" Width="30" Stretch="UniformToFill" Grid.Row="0" Grid.Column="2" />
                    <Image Source="{Binding StarFour}" Height="30" Width="30" Stretch="UniformToFill" Grid.Row="0" Grid.Column="3" />
                    <Image Source="{Binding StarFive}" Height="30" Width="30" Stretch="UniformToFill" Grid.Row="0" Grid.Column="4" />

                </Grid>

                <TextBlock Text="{Binding Time}" Margin="8,14,25,7" VerticalAlignment="Bottom" Style="{StaticResource NewsTimeAgoStyle}" />
            </StackPanel>
        </StackPanel>
    </Grid>
</DataTemplate>

以及“NewsTimeAgoStyle”的样式

<Style x:Key="NewsTimeAgoStyle" TargetType="TextBlock">
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FontSize" Value="20"/>
    <Setter Property="FontFamily" Value="Segoe WP Light" />
    <Setter Property="Margin" Value="0,14,20,0" />
    <Setter Property="Opacity" Value="0.8" />
    <Setter Property="TextWrapping" Value="Wrap" />
    <Setter Property="HorizontalAlignment" Value="Right" />
</Style>

有人知道如何解决这个问题吗?

【问题讨论】:

  • 该绑定的数据源中是否有多余的空格或空白字符串?
  • 不,此绑定的数据源中没有多余的空格或空白字符串(我刚刚检查过)。感谢您的评论!
  • 您可以从一个网格中创建整个块。您的问题是由于堆栈面板的预期行为。不要使用它。将标题移动到网格中,并将其 RowHeight 设置为 *
  • 嘿,谢谢你的建议。我使用了@NullPointer 为我修改的代码,效果很好;)

标签: xaml silverlight windows-phone-8 windows-phone


【解决方案1】:

我不确定,但问题可能是因为您使用 StackPanel 显示标题和时间前文本。我已将您的DataTemplate 更改为使用Grid 面板。试试这个,看看这有什么不同

<DataTemplate>
<Grid Margin="12,2,5,4" >
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Rectangle Fill="{StaticResource PhoneAccentBrush}"
                       Opacity="0.75"
                       Margin="0,0"
                       HorizontalAlignment="Stretch"
                       VerticalAlignment="Stretch"/>

      <Grid Grid.Row="0" 
            Background="Transparent" 
            Height="auto"
            MinHeight="99">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="99"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid HorizontalAlignment="Center" 
                      Background="#FFFFC700">
            <Image Source="{Binding ImageUrl}" 
                   Height="auto" 
                   Width="auto" 
                   Stretch="UniformToFill" 
                   Margin="0" 
                   HorizontalAlignment="Center" 
                   VerticalAlignment="Stretch">
            </Image>
        </Grid>


        <Grid Width="311" Margin="8,-7,0,0" Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <TextBlock Margin="10,15,15,0" Text="{Binding Content}" Style="{StaticResource NewsFeedHighlitedContent}" FontSize="24" />

            <Grid Margin="10,5,0,15" Visibility="{Binding StarsVisibility}" Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30" />
                    <ColumnDefinition Width="30" />
                    <ColumnDefinition Width="30" />
                    <ColumnDefinition Width="30" />
                    <ColumnDefinition Width="30" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>

                <Image Source="{Binding StarOne}" Height="30" Margin="0,0" Width="30" Stretch="UniformToFill" Grid.Row="0" Grid.Column="0" />
                <Image Source="{Binding StarTwo}" Height="30" Width="30" Stretch="UniformToFill" Grid.Row="0" Grid.Column="1" />
                <Image Source="{Binding StarThree}" Height="30" Width="30" Stretch="UniformToFill" Grid.Row="0" Grid.Column="2" />
                <Image Source="{Binding StarFour}" Height="30" Width="30" Stretch="UniformToFill" Grid.Row="0" Grid.Column="3" />
                <Image Source="{Binding StarFive}" Height="30" Width="30" Stretch="UniformToFill" Grid.Row="0" Grid.Column="4" />

            </Grid>

            <TextBlock Text="{Binding Time}" Margin="8,14,25,7" Grid.Row="2" VerticalAlignment="Bottom" Style="{StaticResource NewsTimeAgoStyle}" />
        </Grid>
    </Grid>
</Grid>
</DataTemplate>

【讨论】:

  • 亲爱的空指针,我印象深刻。谢谢您的帮助。此代码效果很好,问题立即解决!谢谢!
猜你喜欢
  • 1970-01-01
  • 2023-04-08
  • 2020-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-25
  • 2018-09-16
  • 1970-01-01
相关资源
最近更新 更多