Текст заголовка сгруппированного CollectionView не работает при использовании ObservableGroupedCollection ⇐ C#
-
Anonymous
Текст заголовка сгруппированного CollectionView не работает при использовании ObservableGroupedCollection
I need a grouped collection view and I would like to bind it to an ObservableGroupedCollection because I am wanting to update the data and my updates to be reflected in the UI. In order to do this I have adapted the example found here
https://github.com/jfversluis/MauiColle ... pingSample but I am having a problem with the collection view headers not rendering the data.
To demonstrate my problem I have created a .net 8 maui app and referenced the nuget package CommunityToolkit.Mvvm version 8.2.2.
Here is my viewmodel
using CommunityToolkit.Mvvm.Collections; using CommunityToolkit.Mvvm.ComponentModel; namespace TestGroupedCollectionView; public partial class PageViewModel: ObservableObject { public ObservableGroupedCollection Animals { get; set; } = []; public PageViewModel() { var bearGroup = new AnimalGroup { GroupName = "Bears" }; var bears = new List { new Animal { Name = "American Black Bear" }, new Animal { Name = "Asian Black Bear" } }; Animals.Add(new ObservableGroup(bearGroup, bears)); var monkeys = new List { new Animal { Name = "Baboon" }, new Animal { Name = "Capuchin Monkey", }, new Animal { Name = "Blue Monkey" }, }; var monkeyGroup = new AnimalGroup { GroupName = "Monkeys" }; Animals.Add(new ObservableGroup(monkeyGroup, monkeys)); } } Here is the animal class
public class Animal { public string Name { get; set; } = ""; } Here is the animal group class
public class AnimalGroup { public string GroupName { get; set; } = ""; } Here is the code behind of my page
namespace TestGroupedCollectionView; public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); BindingContext = new PageViewModel(); } } And here is my xaml code
I have run the code on an emulator, i.e. an Android Studio Pixel 5 and the output you get is:
American Black Bear
Asian Black Bear
Baboon
Capuchin Monkey
Blue Monkey
In other words the group header template is being rendered but the Name text is being left blank. What I am trying to get instead is the following output:
Bears
American Black Bear
Asian Black Bear
Monkeys
Baboon
Capuchin Monkey
Blue Monkey
which as you can see includes the 2 headers, bears and monkeys.
I have tried to add a Datatype like this
But it doesn't make any difference.
Источник: https://stackoverflow.com/questions/781 ... oupedcolle
I need a grouped collection view and I would like to bind it to an ObservableGroupedCollection because I am wanting to update the data and my updates to be reflected in the UI. In order to do this I have adapted the example found here
https://github.com/jfversluis/MauiColle ... pingSample but I am having a problem with the collection view headers not rendering the data.
To demonstrate my problem I have created a .net 8 maui app and referenced the nuget package CommunityToolkit.Mvvm version 8.2.2.
Here is my viewmodel
using CommunityToolkit.Mvvm.Collections; using CommunityToolkit.Mvvm.ComponentModel; namespace TestGroupedCollectionView; public partial class PageViewModel: ObservableObject { public ObservableGroupedCollection Animals { get; set; } = []; public PageViewModel() { var bearGroup = new AnimalGroup { GroupName = "Bears" }; var bears = new List { new Animal { Name = "American Black Bear" }, new Animal { Name = "Asian Black Bear" } }; Animals.Add(new ObservableGroup(bearGroup, bears)); var monkeys = new List { new Animal { Name = "Baboon" }, new Animal { Name = "Capuchin Monkey", }, new Animal { Name = "Blue Monkey" }, }; var monkeyGroup = new AnimalGroup { GroupName = "Monkeys" }; Animals.Add(new ObservableGroup(monkeyGroup, monkeys)); } } Here is the animal class
public class Animal { public string Name { get; set; } = ""; } Here is the animal group class
public class AnimalGroup { public string GroupName { get; set; } = ""; } Here is the code behind of my page
namespace TestGroupedCollectionView; public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); BindingContext = new PageViewModel(); } } And here is my xaml code
I have run the code on an emulator, i.e. an Android Studio Pixel 5 and the output you get is:
American Black Bear
Asian Black Bear
Baboon
Capuchin Monkey
Blue Monkey
In other words the group header template is being rendered but the Name text is being left blank. What I am trying to get instead is the following output:
Bears
American Black Bear
Asian Black Bear
Monkeys
Baboon
Capuchin Monkey
Blue Monkey
which as you can see includes the 2 headers, bears and monkeys.
I have tried to add a Datatype like this
But it doesn't make any difference.
Источник: https://stackoverflow.com/questions/781 ... oupedcolle
Мобильная версия