Giter Club home page Giter Club logo

maui-collection-view-get-started's Introduction

Files to look at:

DevExpress Collection View for .NET MAUI

This repository contains an application that demonstrates the capabilities of the DevExpress Collection View for .NET MAUI library. The DXCollectionView component uses a template to display a collection of data items in a vertical or horizontal list. The component features include the following:

  • Templates for Data Items and Group Headers
  • Data Sorting, Filtering, and Grouping
  • Vertical and Horizontal Scrolling
  • Drag-and-Drop Editing
  • Pull-to-Refresh
  • Infinite Scrolling
  • Swipe Gestures
  • Multiple Item Selection
  • Themes

Prerequisites

  1. Install Visual Studio 2022 and the latest .NET MAUI version. See the following topic on docs.microsoft.com for more information: .NET MAUI Installation.
  2. Register the following NuGet feed in Visual Studio: https://nuget.devexpress.com/free/api.

    If you are an active DevExpress Universal customer, DevExpress Controls for .NET MAUI are available in your personal NuGet feed.

What's in This Repository

An example in this repository allows you to get started with the Collection View component and explore its basic functionality. It demonstrates how to bind the view to a data source, apply an item template, sort, and group data items.

Collection View Collection View

How to Create This Application

The step-by-step instructions below describe how to create an application similar to the application in this repository.

Create a New Project

  1. In Visual Studio 2022, create a new .NET MAUI project. Name it CollectionViewExample.

    If the wizard does not propose a template for .NET MAUI projects, you can call the following command in a CLI to create a new .NET MAUI project:

    dotnet new maui -n CollectionViewExample
    
  2. Install the DevExpress.Maui.CollectionView package from the https://nuget.devexpress.com/free/api NuGet package source.

DevExpress Collection View for .NET MAUI supports iOS and Android. The project should only contain these platforms.

Add a Collection View to the Main Page

In the MauiProgram.cs file, register a handler for the DXCollectionView class.

using Microsoft.Maui;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;
using DevExpress.Maui.CollectionView;

namespace CollectionViewExample {
    public static class MauiProgram {
        public static MauiApp CreateMauiApp() {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureMauiHandlers(handlers => handlers.AddHandler<IDXCollectionView, DXCollectionViewHandler>())
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                });
            return builder.Build();
        }
    }
}

Do the following in the MainPage.xaml file:

  1. Define the dxcv XAML namespace that refers to the DevExpress.Maui.CollectionView CLR namespace.
  2. Remove the default content and add an instance of the DXCollectionView class to the page. Remove the default content's event handlers in the code-behind. We recommend that you remove the default styles (fonts, colors, and other settings) in the App.xaml file.
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:dxcv="clr-namespace:DevExpress.Maui.CollectionView;assembly=DevExpress.Maui.CollectionView"
             x:Class="CollectionViewExample.MainPage"
             BackgroundColor="{DynamicResource PageBackgroundColor}">
    <dxcv:DXCollectionView/>
</ContentPage>

Populate the View with Data

Create the ViewModel and Contact classes as shown below. The ViewModel class exposes the Data property that provides access to the data source. Items in this collection are Contact objects that expose the Name, Photo, and Phone properties.

using Microsoft.Maui.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace CollectionViewExample {
    public class Contact {
        string name;
        public string Name {
            get => this.name;
            set {
                this.name = value;
                if (Photo == null) {
                    string resourceName = value.Replace(" ", "").ToLower() + ".jpg";
                    Photo = ImageSource.FromFile(resourceName);
                }
            }
        }

        public Contact(string name, string phone) {
            Name = name;
            Phone = phone;
        }
        public ImageSource Photo { get; set; }
        public string Phone { get; set; }
    }

    public class ViewModel : INotifyPropertyChanged {
        public List<Contact> Data { get; }
        public ViewModel() {
            Data = new List<Contact>() {
                new Contact("Nancy Davolio", "(206) 555-9857"),
                new Contact("Andrew Fuller", "(206) 555-9482"),
                new Contact("Janet Leverling", "(206) 555-3412"),
                new Contact("Margaret Peacock", "(206) 555-8122"),
                new Contact("Steven Buchanan", "(71) 555-4848"),
                new Contact("Michael Suyama", "(71) 555-7773"),
                new Contact("Robert King", "(71) 555-5598"),
                new Contact("Laura Callahan", "(206) 555-1189"),
                new Contact("Anne Dodsworth", "(71) 555-4444"),
            };
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propertyName = "") {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Do the following in the MainPage.xaml file:

  1. Define the local XML namespace that refers to the CollectionViewExample CLR namespace.
  2. Assign a ViewModel instance to the ContentPage.BindingContext property.
  3. Bind the DXCollectionView.ItemsSource property to the view model's Data property.
  4. Use the DisplayMember property to specify the data field that contains item captions. The DisplayFormat allows you to format captions.
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:dxcv="clr-namespace:DevExpress.Maui.CollectionView;assembly=DevExpress.Maui.CollectionView"
             xmlns:local="clr-namespace:CollectionViewExample"
             x:Class="CollectionViewExample.MainPage"
             BackgroundColor="{DynamicResource PageBackgroundColor}">
    <ContentPage.BindingContext>
        <local:ViewModel/>
    </ContentPage.BindingContext>
    <dxcv:DXCollectionView ItemsSource="{Binding Data}"/>
</ContentPage>

Create an Item Template

The DXCollectionView supports item templates. These templates allow you to specify how each item appears in the view. Make the following changes in the MainPage.xaml file to define an item template:

  1. Assign a DataTemplate to the DXCollectionView.ItemTemplate property.
  2. Populate the template with controls and bind them to data source fields as the markup below demonstrates.
<dxcv:DXCollectionView ItemsSource="{Binding Data}">
        <!--Define the item template.-->
        <dxcv:DXCollectionView.ItemTemplate>
            <DataTemplate>
                <Grid Padding="10, 8, 18, 7">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid Margin="0" Padding="0" ColumnSpacing="0" RowSpacing="0">
                        <Frame BackgroundColor="White"
                                   Padding="0" Margin="0"
                                   HeightRequest="48"
                                   WidthRequest="48"
                                   VerticalOptions="Center"
                                   HorizontalOptions="Center"
                                   HasShadow="False"   
                                   CornerRadius="24">
                            <Frame.IsClippedToBounds>
                                <OnPlatform x:TypeArguments="x:Boolean">
                                    <On Platform="iOS">true</On>
                                    <On Platform="Android">false</On>
                                </OnPlatform>
                            </Frame.IsClippedToBounds>
                            <Image Source="{Binding Photo}"/>
                        </Frame>
                        <Ellipse Margin="0"
                                 Fill="Transparent"
                                 Stroke="LightGray" 
                                 StrokeThickness="1"
                                 HeightRequest="50"
                                 WidthRequest="50"
                                 VerticalOptions="Center"
                                 HorizontalOptions="Center">
                        </Ellipse>
                    </Grid>
                    <StackLayout Grid.Column="1"
                             Padding="18,1,18,7"
                             Orientation="Vertical">
                        <Label Text="{Binding Name}"
                           Margin="0,2"
                           TextColor="#55575c"/>
                        <Label Text="{Binding Phone}"
                               TextColor="#959aa0"/>
                    </StackLayout>
                </Grid>
            </DataTemplate>
        </dxcv:DXCollectionView.ItemTemplate>
        
        <!--Specify margins.-->
        <dxcv:DXCollectionView.Margin>
            <OnIdiom x:TypeArguments="Thickness" Phone="16,0,0,0" Tablet="71,0,0,0"/>
        </dxcv:DXCollectionView.Margin>
</dxcv:DXCollectionView>

Do the following to add contact photos to the solution:

  1. Download this repository.
  2. Copy images from the /CollectionViewExample/Resources/Images folder in the downloaded repository to the same folder in your project.

Run the application. The Collection View now displays a photo, name, and phone number for each contact.

Collection View - Item Template Collection View - Item Template

Sort Data Items

Make the following changes in the MainPage.xaml file to sort data items:

  1. Create a SortDescription object, and specify its FieldName and SortOrder properties.
  2. Add this object to the DXCollectionView.SortDescriptions collection.
<dxcv:DXCollectionView ItemsSource="{Binding Data}">
        <!-- Define ItemTemplate here.-->

        <!--Sort items.-->
        <dxcv:DXCollectionView.SortDescriptions>
            <dxcv:SortDescription FieldName="Name" SortOrder="Descending"/>
        </dxcv:DXCollectionView.SortDescriptions>
</dxcv:DXCollectionView>

Run the application. Contacts are now sorted by first name in descending order.

Collection View - Sort Data Items Collection View - Sort Data Items

You can also sort list items by multiple data fields. To do this, create a SortDescription object for each field that should be sorted. The order of these objects in the DXCollectionView.SortDescriptions collection defines the sort order in the view.

Group Data Items

Make the following changes in the MainPage.xaml file to group data items:

  1. Assign a GroupDescription object to the DXCollectionView.GroupDescription property. Set the GroupDescription.FieldName property to Name and GroupDescription.GroupInterval to Alphabetical.
  2. Use the DXCollectionView.GroupHeaderTemplate property to specify group header appearance.
<dxcv:DXCollectionView ItemsSource="{Binding Data}">
        <!-- Define ItemTemplate here.-->

        <!--Group items.-->
        <dxcv:DXCollectionView.GroupDescription>
            <dxcv:GroupDescription FieldName="Name" GroupInterval="Alphabetical"/>
        </dxcv:DXCollectionView.GroupDescription>

        <!--Define the group header template.-->
        <dxcv:DXCollectionView.GroupHeaderTemplate>
            <DataTemplate>
                <StackLayout Margin="2, 0, 18, 10">
                    <Label FontFamily="Roboto-Medium"
                       Margin="0,20,0,1"
                       TextColor="#55575c"
                       Text="{Binding Value}"/>
                    <BoxView BackgroundColor="#ebebeb" 
                         HeightRequest="1"/>
                </StackLayout>
            </DataTemplate>
        </dxcv:DXCollectionView.GroupHeaderTemplate>
</dxcv:DXCollectionView>

Run the application. Contacts whose first name begins with the same letter are now arranged into groups. Each group is identified by a header. Users can tap group headers to expand or collapse groups.

Collection View - Group Data Items Collection View - Group Data Items

Known Issues

maui-collection-view-get-started's People

Contributors

alexeyzhukoff avatar gorbounoff avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.