Hello,
Welcome to our Microsoft Q&A platform!
Do you want to achieve the result like following screenshot?
If so, I notice you have three static properties in the Groceries.cs. If you want to achieve the grouping in the listview, do not need these property. change it like following code.
public class Groceries : ObservableCollection<string>
{
public string Category { get; set; }
public Groceries(string s)
{
Category = s;
}
}
Then, we can add data in the viewModel.
class OrganizedViewModel
{
public ObservableCollection<Groceries> OGroupedList { get; set; }
public string Category { get; set; }
public OrganizedViewModel()
{
OGroupedList = new ObservableCollection<Groceries>();
var groceriesFruit = new Groceries("Fruit") { "Fruit1", "Fruit2" };
var grocerieVegetables = new Groceries("Vegetables") { "Vegetables1", "Vegetables2" };
OGroupedList.Add(groceriesFruit);
OGroupedList.Add(grocerieVegetables);
}
}
In the layout's background code. You can add bindingContext.
public partial class Page2 : ContentPage
{
public Page2()
{
InitializeComponent();
BindingContext = new OrganizedViewModel();
}
}
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.