<ListView
ItemsSource="{Binding Messages}"
Grid.ColumnSpan="2"
HeightRequest="100">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout>
<Label
Text="{Binding When}"
XAlign="Center"/>
<StackLayout
BackgroundColor="Gray"
Orientation="Vertical"
VerticalOptions="Center">
<Label
Text="{Binding Message}"
XAlign="Start"/>
<Label
Text="{Binding Sender}"
XAlign="Start"
TextColor="Red"/>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我在 Xamarin.Forms 应用程序中呈现自定义 ListView。
StackLayout
其中包含两个 Label
s(已绑定(bind)“Message”和“Sender”),当前仅显示一个 child 。使用上面的代码,它只显示“消息”。如果我将代码更改为<StackLayout
BackgroundColor="Gray"
Orientation="Vertical"
VerticalOptions="Center">
<Label
Text="{Binding Sender}"
XAlign="Start"
TextColor="Red"/>
<Label
Text="{Binding Message}"
XAlign="Start"/>
</StackLayout>
它只显示发件人。简而言之,它只显示第一个 child 。我在这里做错了什么?
最佳答案
问题与@Grisha 指出的类似。事实证明 RowHeight 更小,第二个 StackLayout 被剪裁了。
解决方案是设置 HasUnevenRows
ListView
的属性(property)成为 TRUE
.因此,RowHeight
是自动计算的。
关于xaml - ListView 中的 Xamarin.Forms Vertical StackLayout 仅显示一个(第一个)子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30163537/