如何让绑定数据的BindableLayout实现滚动?无需使用ListView
解决BindableLayout列表无法滚动的问题
你的列表无法滚动的核心原因是StackLayout设置了固定的HeightRequest="255"——这个属性会强制StackLayout的高度固定为255,即使列表内容超过这个高度,StackLayout也不会自动扩展,导致ScrollView无法检测到内容超出可视范围,自然不会触发滚动。
修改方案:
直接移除StackLayout的HeightRequest="255"属性,让StackLayout根据绑定的列表内容自动计算高度。当内容高度超过ScrollView的可视区域时,滚动功能就会正常生效。
修改后的完整代码:
<Grid Grid.Row="0" IsVisible="{Binding SearchListLayout}" Margin="0" Padding="0" BackgroundColor="White"> <ScrollView> <StackLayout BindableLayout.ItemsSource="{Binding ProductsList}" x:Name="SearchLayoutItemList"> <BindableLayout.ItemTemplate> <DataTemplate> <Grid RowDefinitions="37,Auto"> <Image Grid.Row="0" Source="searchingicon.png" Style="{DynamicResource icons}"/> <Label Grid.Row="0" Text="{Binding name}" FontSize="14" TextColor="Black" Margin="50,0,0,0" HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand"/> <Image Grid.Row="0" Source="reload.png" Style="{DynamicResource icons}" HorizontalOptions="EndAndExpand" Margin="0,0,15,0"/> <BoxView Grid.Row="1" Style="{DynamicResource DarkSeparator}" /> </Grid> </DataTemplate> </BindableLayout.ItemTemplate> </StackLayout> </ScrollView> </Grid>
额外说明:
如果需要限制列表的最大显示高度(避免列表过长占据过多屏幕空间),不要给StackLayout设置固定高度,而是给外层的ScrollView添加MaximumHeightRequest属性,这样当内容超过最大高度时依然能正常滚动:
<ScrollView MaximumHeightRequest="255"> <StackLayout BindableLayout.ItemsSource="{Binding ProductsList}" x:Name="SearchLayoutItemList"> <!-- 列表项模板 --> </StackLayout> </ScrollView>
内容的提问来源于stack exchange,提问作者Abhi
相关产品推荐
相关产品推荐

