【问题标题】:CollectionView not showing when using grouping使用分组时 CollectionView 不显示
【发布时间】:2021-06-07 17:53:59
【问题描述】:

关于创建CollectionViewObservableRangeCollection 分组列表时我缺少什么的任何想法?目前,没有任何显示。

我正在关注参考:https://github.com/jamesmontemagno/MyCoffeeApp/blob/master/MyCoffeeApp/MyCoffeeApp/ViewModels/CoffeeEquipmentViewModel.cs

    <CollectionView ItemsSource="{Binding CategoryListGroup }"
                    IsGrouped="True" 
                    SelectionMode="Single"
                    SelectionChanged="CollectionView_SelectionChanged">
        <CollectionView.GroupHeaderTemplate>
            <DataTemplate>
                <StackLayout>
                    <Label Text="{Binding Key}"></Label>
                </StackLayout>
            </DataTemplate>
        </CollectionView.GroupHeaderTemplate>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <ContentView>
                    <Grid Padding="0">
                        <Frame CornerRadius="3" BorderColor="#f2f4f5" HasShadow="True">
                            <StackLayout Orientation="Horizontal">
                                <Image Source="icon_about"
                                           WidthRequest="25"  />
                                <StackLayout VerticalOptions="Center">
                                    <Label VerticalOptions="Center"
                                                FontSize="16" 
                                                Text="{Binding Name}" />
                                </StackLayout>
                            </StackLayout>
                        </Frame>
                    </Grid>
                </ContentView>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

后端代码:这里我创建了一个列表,然后放入分组列表中

  ObservableRangeCollection<CategoryModel> CategoryList { get; set; }
 ObservableRangeCollection<Grouping<string, CategoryModel>> CategoryListGroup { get; set; }

   CategoryList = new ObservableRangeCollection<CategoryModel>();
        CategoryList.Add(new CategoryModel { Category = "Cat1", Name = "random text" });
        CategoryList.Add(new CategoryModel { Category = "Cat1", Name = "random text" });
        CategoryList.Add(new CategoryModel { Category = "Cat2", Name = "random Text" });
        CategoryList.Add(new CategoryModel { Category = "Cat2", Name = "random text" });
        CategoryList.Add(new CategoryModel { Category = "Cat3", Name = "random text" });
        CategoryList.Add(new CategoryModel { Category = "Cat3", Name = "random text" });

        CategoryListGroup = new ObservableRangeCollection<Grouping<string, CategoryModel>>();
            CategoryListGroup.Add(new Grouping<string, CategoryModel>("Cat1", CategoryList.Where(c => c.Category == "Cat1")));
            CategoryListGroup.Add(new Grouping<string, CategoryModel>("Cat2", CategoryList.Where(c => c.Category == "Cat2")));
            CategoryListGroup.Add(new Grouping<string, CategoryModel>("Cat3", CategoryList.Where(c => c.Category == "Cat3")));

  BindingContext = this;

【问题讨论】:

  • ItemsSource="{Binding CategoryList }" - CategoryList 不是分组数据
  • 抱歉,发布此问题时打错字。我编辑了它。顺便说一句CategoryList 工作正常

标签: c# android xamarin xamarin.forms collectionview


【解决方案1】:

发现问题...我必须将model class 设置为public 并将ObservableRangeCollection 设置为public 以及

public ObservableRangeCollection<CategoryModel> CategoryList { get; set; }
 public ObservableRangeCollection<Grouping<string, CategoryModel>> CategoryListGroup { get; set; }

    public class CategoryModel
    {
        public string Category { get; set; }
        public string Name { get; set; }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多