question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sample ListView IsGroupingEnabled

See original GitHub issue

Hi @adospace I am having a hard time creating the ItemsSource for ListView with IsGroupingEnabled(true).

I understand that without grouping we return one ViewCell so the internal system can reuse the ViewCells. Why does IsGroupingEnabled also return a ViewCell?

Syntactically this works but I am confused as to what should ViewCell return?

new ListView()
                .IsGroupingEnabled(true)
                .ItemsSource(State.GroupedItems, RenderGroup)
                .HFill()
                .VFill()
:
:

private ViewCell RenderGroup(GroupedCollection<string, Contact> groupedContacts) => new ViewCell
{
};

I am trying to recreate the standard iOS grouped contact list:

Issue Analytics

  • State:closed
  • Created 4 months ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
adospacecommented, May 10, 2023

Fixed in 1.0.128

4reactions
adospacecommented, May 9, 2023

Just pushed some fixes and enhancements for the ListView. I’ve added a sample page to the Test App project showing how to group list view items. If you need the Nuget package please wait a couple of hours.

MauiReactor_ListView

class ListViewExtendedTestPageState
{
    public Person? SelectedPerson { get; set; }
}

class ListViewExtendedTestPage : Component<ListViewExtendedTestPageState>
{
    public override VisualNode Render()
    {
        return new ContentPage("ListView Extended Test (BETA)")
        {
            new ListView(MauiControls.ListViewCachingStrategy.RecycleElementAndDataTemplate)
                .IsGroupingEnabled(true)
                .ItemsSource<ListView, GroupOfPerson, Person>(GroupOfPerson.All, RenderGroup, RenderItem)
                .SeparatorVisibility(MauiControls.SeparatorVisibility.None)
                .OnItemSelected(OnSelectedItem)
                //NOTE: Header/Footer not working under .net 7
                //https://github.com/dotnet/maui/issues/13560
                //https://github.com/dotnet/maui/issues/12312
                //.Header(new Label("Header"))
                //.Footer(new Label("Footer"))
        };
    }

    private void OnSelectedItem(object? sender, SelectedItemChangedEventArgs args)
    {
        SetState(s => s.SelectedPerson = args.SelectedItem as Person);
    }

    private ViewCell RenderGroup(GroupOfPerson person)
    {
        return new ViewCell
        {
            new Label(person.Initial)
                .FontSize(14.0)
                .FontAttributes(MauiControls.FontAttributes.Bold)
                .Margin(5)
                .BackgroundColor(Colors.LightGray),
        };
    }

    private ViewCell RenderItem(Person person)
    {
        return new ViewCell
        {
            new Label($"{person.FirstName} {person.LastName}")
                .FontSize(14.0)
                .FontAttributes(MauiControls.FontAttributes.Bold)
                .Padding(5)
                .VerticalTextAlignment(TextAlignment.Center)
        };
    }
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

ListView Appearance - Xamarin
This article explains how to customize ListViews in Xamarin.Forms applications by using headers, footers, groups, and variable height cells.
Read more >
Xamarin.Forms - ListView Grouping - Code Samples
Sample code associated with the ListView Docs: This sample demonstrates how to implement grouping in a ListView, using both XAML and C (UI)...
Read more >
Xamarin Forms ListView Grouping
The ListView control is the go to control for displaying a list of data in Xamarin Forms. It contains a number of features, ......
Read more >
ListView grouping - The complete WPF tutorial
Adding grouping to the WPF ListView is very simple - all you need is a GroupStyle with a HeaderTemplate, to tell the ListView...
Read more >
Xamarin Forms ListView Grouping Contents not Binding
I am having issues using ListView Grouping to get the content of my lists to display in labels. The ListView's ItemsSource and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found