You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

切换CollectionView布局后项被拉伸至屏幕宽度的问题排查

问题:CollectionView切换布局后水平模式条目空间异常

我正在实现一个「查看更多」按钮,用来切换条目显示模式:

  • 「更少」模式:水平列表,可同时展示多个条目
  • 「更多」模式:垂直双列网格,占满整个屏幕

初始启动时布局正常,能显示多个条目,但切换为垂直布局后再切回水平布局,所有条目会占用远超预期的空间。

所用CollectionView的XAML代码

<CollectionView
    ItemsSource="{Binding FoodPlace}"
    x:Name="CollectionviewFoodplaces"
    HorizontalOptions="Start"
    ItemsLayout="HorizontalList"
    SelectionMode="Single">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Grid x:Name="foodgrid" RowDefinitions="100,50" ColumnDefinitions="150" MaximumWidthRequest="150">
                <Image Source="{Binding Icon}"
                       x:Name="Icon_"
                       WidthRequest="150"
                       Grid.RowSpan="2" 
                       Grid.Column="0"
                       Margin="2"/>
                <Label Text="{Binding Path=Pin.Label}" 
                       x:Name="Labell"
                       Grid.Row="1" 
                       Margin="2" 
                       HorizontalOptions="Center"
                       FontSize="Body" 
                       FontAttributes="Bold"
                       WidthRequest="150"
                       Grid.Column="0"/>
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

切换布局的按钮后台代码

public static GridItemsLayout seemorelayout = new GridItemsLayout(2, ItemsLayoutOrientation.Vertical);
...
...
...
private void buttonFoodplaceSeeMore_Clicked(object sender, EventArgs e)
{
    SeeMoreFood = !SeeMoreFood;
    if (SeeMoreFood == true)
    {
        buttonFoodplaceSeeMore.Text = "See Less <";
        CollectionviewFoodplaces.ItemsLayout = seemorelayout;

    }
    if (SeeMoreFood == false)
    {
        buttonFoodplaceSeeMore.Text = "See More >";
        CollectionviewFoodplaces.ItemsLayout = LinearItemsLayout.Horizontal;

    }
}

内容的提问来源于stack exchange,提问作者user23693962

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 20:51:06