C# 如何更改ViewCell中的高度
我正在尝试更改listview上的ViewCell,但下面的代码不适用于我:C# 如何更改ViewCell中的高度,c#,xaml,xamarin-forms,C#,Xaml,Xamarin Forms,我正在尝试更改listview上的ViewCell,但下面的代码不适用于我: <DataTemplate> <ViewCell Height="100"> <StackLayout Orientation="Horizontal"> <Image Source="{Binding Seller.Thumbnail}}" Aspect="AspectFit" /> <Sta
<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>
高度/重量属性在Xamarin中大部分是只读的。您无法设置ViewCell的大小
但是,您可以更改StackLayout的HeightRequest属性以获得所需的内容。设置ViewCell的高度应该可以 尝试将StackLayout的
垂直选项
和水平选项
设置为FillAndExpand
- 如果所有单元格都设置了相同的大小,则 ListView本身
- 如果要改为设置
然后将ViewCell.Height
设置为ListView.hasRows
(但它会对性能产生一些影响)true
<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>
仅当ListView.HasUnderrows
或TableView.HasUnderrows
属性设置为true
时,为视图单元格设置高度将有效。2019年正确的做法是将此设置为固定高度:
<ListView RowHeight="100" />
如果不希望在所有行中固定高度,请使用:
<ListView HasUnevenRows="true" />
“一般”是这里的关键术语。然而,对于ViewCell,它是get/set。如果您查看VisualStudio中该属性上弹出的intellisense,这一点已得到确认。您必须记住,ListView本身的行高优先。因此,设置CellView的.Height似乎没有任何影响。它主要用于与ListVIew结合使用。hasRows=true
有关ListVIew.RowHeight与ViewCell.Height的讨论,请参见。重要提示:此答案适用于可能会换行到多行文本的标签。如果这样做,请不要忘记上面代码片段中的haslunterrows=“True”。True,但请参阅上面Daniel Luberda更完整的答案,上面已经说过。设置haslunterrows=“True”
才是真正的答案