如何在ViewCell中改变高度

21

我正在尝试在ListView上更改ViewCell,但是下面的代码对我没有起作用:

<DataTemplate>
    <ViewCell Height="100">
        <StackLayout Orientation="Horizontal">
            <Image Source="{Binding Seller.Thumbnail}}" Aspect="AspectFit" />
            <StackLayout Orientation="Vertical" >
                <Label Text="{Binding CouponName}" FontAttributes="Bold" FontSize="12" />
                <Label Text="{Binding EndOffer}" FontSize="11" />
            </StackLayout>
        </StackLayout>
    </ViewCell>
</DataTemplate>
6个回答

70
  • 如果所有单元格的大小相同,则将ListView.RowHeight属性设置在ListView本身上。
  • 如果您想要设置ViewCell.Height而不是ListView.RowHeight,则将ListView.HasUnevenRows设置为true(但它会对性能产生一定影响)。

7
在这里,设置HasUnevenRows="true"是真正的答案。 - derekantrican

16

在2019年,修正高度的正确方法是将以下内容放置:

<ListView RowHeight="100" />

如果您不想在所有行中固定高度,请使用:

<ListView HasUnevenRows="true" />

12

仅当ListView.HasUnevenRowsTableView.HasUnevenRows 属性设置为true时,设置 ViewCell 的高度才会生效。


真的,但请参考Daniel Luberda上面更完整的答案,他已经表述了这一点。 - ToolmakerSteve

5

设置ViewCell的高度应该可以实现。

尝试将StackLayout的VerticalOptionsHorizontalOptions设置为FillAndExpand


请参考Daniel Luberda的回答,讨论ListView.RowHeight与ViewCell.Height的区别。 - ToolmakerSteve

4
只需在包含标签的位置设置网格(Grid)的行定义(RowDefinition)的高度(Height)为自动(Auto),像这样:
<ListView ItemsSource="{Binding Gastos}" HasUnevenRows="True" SeparatorVisibility="None">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell >
                <Grid Padding="5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Label Text="{Binding Id}" VerticalOptions="Start"/>
                    <Label Grid.Column="1" Text="{Binding Descripcion}" LineBreakMode="WordWrap"/>
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

重要提示:此答案适用于标签可能换行多行文本的情况。如果是这样,请不要忘记加上**HasUnevenRows="True"**,就像上面的代码片段中所见。 - ToolmakerSteve

-2

在 Xamarin 中,高度/重量属性大多是只读的。您无法设置 ViewCell 的大小。

但是,您可以更改 StackLayout 的 HeightRequest 属性以实现您想要的效果。


3
这里的关键术语是“generally”。但在ViewCell的情况下,它是get/set。如果您查看Visual Studio中该属性弹出的智能感知,可以确认这一点。但您必须记住,ListView本身的RowHeight将优先于此。因此,设置CellView的.Height可能不会产生任何影响。它主要是用于与 ListVIew.HasUnevenRows=true 结合使用。 - Clint StLaurent

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接