简体   繁体   English

GridView 项目高度是固定的

[英]GridView item height is fixed

I am working on a project where I have a GridView and I need to show an image and 3 TextBlock s.我正在做一个项目,我有一个GridView ,我需要显示一个图像和 3 个TextBlock The image has a fixed Height and Width properties and the first TextBlock have either 1 line of text or 2. The other TextBlock s have only 1 line of text.该图像具有固定的HeightWidth属性,第一个TextBlock具有 1 行文本或 2 行文本。其他TextBlock仅具有 1 行文本。

The problem is that if the first TextBlock has 1 line of text everything is shown perfectly but if the text is longer than one line the last TextBlock is not shown in the GridViewItem and it is pushed down where it gets hidden.问题是,如果第一个TextBlock有 1 行文本,则所有内容都会完美显示,但如果文本长于一行,则最后一个TextBlock不会显示在GridViewItem中,并且会被向下推到隐藏的位置。

<GridView ItemsSource="{Binding HomeList, Mode=OneWay}">
    <GridView.ItemsPanel>
        <ItemsPanelTemplate>
            <ItemsWrapGrid Orientation="Horizontal" HorizontalAlignment="Center"/>
        </ItemsPanelTemplate>
    </GridView.ItemsPanel>
    <GridView.ItemTemplate>
        <DataTemplate x:DataType="local:ItemHelper">
            <UserControl>
                <StackPanel>
                    <Image Name="image" Source="{x:Bind ItemImage}"
                           Height="144" Width="256" HorizontalAlignment="Center"/>
                    <TextBlock Name="title" Text="{x:Bind ItemTitle}" Style="{StaticResource OneLinedItemTitle}"/>
                    <TextBlock Name="section" Text="{x:Bind ItemSection}"/>
                    <TextBlock Name="pubUpdate" Text="{x:Bind ItemPublishTime}"/>
                </StackPanel>
            </UserControl>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

I have also tried the above XAML with RelativePanel and the same thing happens.我也用RelativePanel尝试了上面的 XAML,同样的事情发生了。 I want the height to be fixed at max height with 2 lines of text for title or adjustable for each item in the GridView .我希望将高度固定在最大高度,并为GridView中的每个项目添加 2 行文本作为title或进行调整。

图片 1

As you can see that the first item shows 3 TextBlock s but the 2nd item shows only 2 where the first one has 2 lines of text.如您所见,第一项显示 3 个TextBlock ,但第二项仅显示 2 个,其中第一项有 2 行文本。

I only see the following possible options:我只看到以下可能的选项:

  1. Set the height of the StackPanel to be fixed for the max height of the itemsStackPanel的高度设置为固定为项目的最大高度
  2. Set the height of the title to be fixed with 2 lines of text设置title高度固定为2行文字
  3. Maybe editing the default Style for GridViewItem would help也许编辑GridViewItem的默认Style会有所帮助

Or maybe there is any other possible option which doesn't involve setting the fixed height for any element.或者可能有任何其他可能的选项不涉及为任何元素设置固定高度。

Any suggestions?有什么建议么? Thanks.谢谢。

Why don't you use a Grid inside your UserControl instead of the StackPanel .为什么不在UserControl中使用Grid而不是StackPanel The StackPanel sizes to content Heights only. StackPanel的大小仅为内容Heights I would use a Grid , set the RowDefinition at third Row to Height="*" and the other ones to Height="Auto" .我会使用Grid ,将第三RowRowDefinition设置为Height="*" ,将其他行设置为Height="Auto" I tested this with an ItemsControl (that worked...)我用ItemsControl对此进行了测试(有效...)

<GridView ItemsSource="{Binding HomeList, Mode=OneWay}">
    <GridView.ItemsPanel>
        <ItemsPanelTemplate>
            <ItemsWrapGrid Orientation="Horizontal" HorizontalAlignment="Center"/>
        </ItemsPanelTemplate>
    </GridView.ItemsPanel>
    <GridView.ItemTemplate>
        <DataTemplate x:DataType="local:ItemHelper">
            <UserControl>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Image Grid.Row="0" Name="image" Source="{x:Bind ItemImage}"                          Height="144" Width="256" HorizontalAlignment="Center"/>
                    <TextBlock Grid.Row="1" Name="title" Text="{x:Bind ItemTitle}" Style="{StaticResource OneLinedItemTitle}"/>
                    <TextBlock Grid.Row="2" Name="section" Text="{x:Bind ItemSection}" TextWrapping="Wrap"/>
                    <TextBlock Grid.Row="3" Name="pubUpdate" Text="{x:Bind ItemPublishTime}"/>
                </Grid>
            </UserControl>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

It seems the first item is not rendering correctly (at least this is what happened to me).似乎第一项没有正确呈现(至少这是发生在我身上的事情)。 Check Justin XL answer , maybe it can help.检查Justin XL answer ,也许它可以提供帮助。 Apparently the problem (from poor rendering) is solved by setting the ItemHeight property显然问题(由于渲染不佳)通过设置ItemHeight属性解决

<GridView.ItemsPanel>
     <ItemsPanelTemplate>
          <ItemsWrapGrid  ItemHeight="DesiredHeight"/>
     </ItemsPanelTemplate>
</GridView.ItemsPanel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM