【发布时间】:2021-06-07 17:53:59
【问题描述】:
关于创建CollectionView 和ObservableRangeCollection 分组列表时我缺少什么的任何想法?目前,没有任何显示。
<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