🏗️
MauiReactor
  • What is MauiReactor?
  • What's new in Version 2
  • What's new in Version 3
  • Getting Started
  • Getting Started Version 2
  • Components
    • State-less Components
    • Stateful Components
      • Inline Components
    • Component life-cycle
    • Component Properties
    • Component with children
    • Component Parameters
    • Theming
    • Navigation
      • NavigationPage
      • Shell
      • Back button
    • Controls
      • Button
      • RadioButton
      • FlyoutPage
      • CollectionView
        • Interactions
        • Layout
        • Selection
        • Empty view
        • Scrolling
        • Grouping
      • IndicatorView
      • Picker
      • Shell
      • Label
    • Wrap 3rd party controls
      • Lottie animations
      • Provide DataTemplate to native controls
    • Accessing native controls
    • Animation
      • Property-Based
      • AnimationController
      • Timer
    • Graphics
      • CanvasView control
    • Window
    • Testing
    • XAML Integration
  • Deep dives
    • Native tree and Visual tree
    • Dependency injection
    • Working with the GraphicsView
    • Migrating from MVVM Model
    • Using XAML Resources
    • Behaviors
  • resources
    • Source and Sample Applications
  • Q&A
    • How to deal with state shared across Components?
    • Does this support ObservableCollection for CollectionView?
    • Do we need to add states to create simple animations such as ScaleTo, FadeTo, etc on tap?
    • How to deal with custom dialogs/popups?
  • How to create a Menu/ContextMenu?
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Components
  2. Controls
  3. CollectionView

Grouping

This page shows how to bind a grouped list of items to a CollectionView

Large data sets can often become unwieldy when presented in a continually scrolling list. In this scenario, organizing the data into groups can improve the user experience by making it easier to navigate the data.

In MauiReactor you can create a grouped view of your collection using an overload of the ItemsSource prop method as shown in the example below:

public class Animal
{
    public string Name { get; set; }
    public string Location { get; set; }
    public string Details { get; set; }
    public string ImageUrl { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

public class AnimalGroup : List<Animal>
{
    public string Name { get; private set; }

    public AnimalGroup(string name, List<Animal> animals) : base(animals)
    {
        Name = name;
    }

    public override string ToString()
    {
        return Name;
    }
}

class MainPageGroupingState
{
    public IEnumerable<AnimalGroup> Animals { get; set; }
}

class MainPageGrouping : Component<MainPageGroupingState>
{
    protected override void OnMounted()
    {
        State.Animals = CreateAnimalsCollection(false);

        base.OnMounted();
    }

    public override VisualNode Render()
    {
        return new ContentPage
        {
            new CollectionView()
                .IsGrouped(true)
                .ItemsSource<CollectionView, AnimalGroup, Animal>(State.Animals, RenderPerson, RenderHeader, RenderFooter)
        };
    }
    private VisualNode RenderPerson(Animal animal)
    {
        return new Grid("*,*", "64, *")
        {
            new Image(animal.ImageUrl)
                .GridRowSpan(2),

            new Label(animal.Name)
                .GridColumn(1),

            new Label(animal.Location)
                .GridRow(1)
                .GridColumn(1)
                .FontSize(12)
                .TextColor(Colors.Gray)
        }
        .HeightRequest(68)
        .Padding(4);
    }

    private VisualNode RenderHeader(AnimalGroup group)
    {
        return new Label(group.Name)
            .Padding(5)
            .FontSize(40)
            .BackgroundColor(Colors.Gray)
            .TextColor(Colors.White);
    }

    private VisualNode RenderFooter(AnimalGroup group)
    {
        return new Label($"Animals in the group: {group.Count}")
            .Padding(5);
    }


    private static IEnumerable<AnimalGroup> CreateAnimalsCollection(bool includeEmptyGroups)
    {
        ...

        return animals;
    }

}
PreviousScrollingNextIndicatorView

Last updated 1 year ago

Was this helpful?

A CollectionView with group headers and footers