我想知道这是否是框架中的潜在错误。除非满足以下两个条件之一,否则ListView DataTemplate中的Button对象似乎不会生成:
示例:
<ListView itemSource="{Binding SomeExampleCollection}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout>
<Label Text="{Binding SomeStringProperty}" />
<Label Text="these 3 labels and button below APPEARS"/>
<Button Text="{Binding SomeStringProperty}"></Button>
<Label Text="Button below this label will NOT appear"/>
<Button Text="does NOT appear"></Button>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
如条件1所述,这将工作/生成按钮:
<ListView itemSource="{Binding SomeExampleCollection}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout>
<Button Text="DOES appear"></Button>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我是否遗漏了文档中的内容或明显的内容?这对我来说似乎有些麻烦。
环境详情: 在VS 2017 CE中调试PCL项目 设备:三星SM-G900V(Android 6.0 - API 23)Galaxy s5
答案 0 :(得分:0)
如果您不是绝对需要,我建议不要使用HasUnevenRows="true"
,因为它会降低您的ListView的性能。您所要做的就是将ListView上的RowHeight设置为某个值。例如:
<ListView ItemsSource="{Binding SomeExampleCollection}" RowHeight="200">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding SomeStringProperty}" />
<Label Text="these 3 labels and button below APPEARS"/>
<Button Text="{Binding SomeStringProperty}"/>
<Label Text="Button below this label will NOT appear"/>
<Button Text="does NOT appear"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
如果按钮仍然没有显示,那是因为RowHeight
还不够大。根据标签上文本的长度,当在带有StackLayout的ListView内部时,如果您的RowHeight不够大,它会将其从任何其他视图中删除。
此外,可能值得将其从StackLayout
切换为Grid
,其中包含您指定的行Grid.RowDefinitions
及其Height
以确切知道ListView RowHeight的大小应该是。
<ListView ItemsSource="{Binding SomeExampleCollection}" RowHeight="180">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="20" />
<RowDefinition Height="60" />
<RowDefinition Height="20" />
<RowDefinition Height="60" />
</Grid.RowDefinitions>
<Label Text="{Binding SomeStringProperty}" />
<Label Grid.Row="1" Text="these 3 labels and button below APPEARS" />
<Button Grid.Row="2" Text="{Binding SomeStringProperty}" />
<Label Grid.Row="3" Text="Button below this label will NOT appear" />
<Button Grid.Row="4" Text="does NOT appear" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>