Getting Started with MAUI Accordion (SfAccordion)

26 Jan 202418 minutes to read

This section provides a quick overview of how to get started with the Accordion (SfAccordion) for .NET MAUI. Walk-through the entire process of creating the real world of this control.

To get start quickly with .NET MAUI Accordion, you can check on this video:

Creating an application using the .NET MAUI Accordion

  1. Create a new .NET MAUI application in Visual Studio.
  2. Syncfusion .NET MAUI components are available on nuget.org. To add SfAccordion to your project, open the NuGet package manager in Visual Studio, search for Syncfusion.Maui.Expander and install it.
  3. Import the control namespace Syncfusion.Maui.Accordion in XAML or C# code.
  4. Initialize the SfAccordion control.
<ContentPage   
    . . .
      xmlns:syncfusion="clr-namespace:Syncfusion.Maui.Accordion;assembly=Syncfusion.Maui.Expander">
    <syncfusion:SfAccordion />
</ContentPage>
using Syncfusion.Maui.Accordion;
. . .
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        SfAccordion accordion = new SfAccordion();
    }
}

Register the handler

The Syncfusion.Maui.Core NuGet package is a dependency for all Syncfusion controls in .NET MAUI. In the MauiProgram.cs file, you need to register the handler for Syncfusion core.

using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Controls.Xaml;
using Microsoft.Maui.Hosting;
using Syncfusion.Maui.Core.Hosting;

namespace GettingStarted
{
    public class MauiProgram 
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                });

            builder.ConfigureSyncfusionCore();
            return builder.Build();
        }
    }
}

Defining accordion items

The SfAccordion is a layout control with a vertically stacked list of accordion items that comprise a header and content. You can load any view in the header and content sections. Users can expand or collapse the content view by tapping header.

In this example, a Grid is loaded in both the header and content of accordion items.

NOTE

When loading Label as direct children of Header or Content of AccordionItem, then it will lead to an exception. So, load Label inside Grid to overcome the crash.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:GettingStarted"
             x:Class="GettingStarted.MainPage"
             xmlns:syncfusion="clr-namespace:Syncfusion.Maui.Accordion;assembly=Syncfusion.Maui.Expander">
<ContentPage.Content>
        <syncfusion:SfAccordion >
            <syncfusion:SfAccordion.Items>
                <syncfusion:AccordionItem>
                    <syncfusion:AccordionItem.Header>
                        <Grid  HeightRequest="48">
                            <Label Text="Robin Rane" Margin="16,14,0,14" CharacterSpacing="0.25" FontFamily="Roboto-Regular"  FontSize="14" />
                        </Grid>
                    </syncfusion:AccordionItem.Header>
                    <syncfusion:AccordionItem.Content>
                        <Grid ColumnSpacing="10" RowSpacing="2" BackgroundColor="#f4f4f4"  >
                            <Grid Margin="16,6,0,0">
                                <Grid.Resources>
                                    <Style TargetType="Label">
                                        <Setter Property="FontFamily" Value="Roboto-Regular"/>
                                    </Style>
                                </Grid.Resources>
                                <Grid.RowDefinitions >
                                    <RowDefinition Height="25"/>
                                    <RowDefinition Height="25"/>
                                    <RowDefinition Height="25"/>
                                    <RowDefinition Height="25"/>
                                    <RowDefinition Height="{OnPlatform Default=90,Android=90,WinUI=70, iOS=100,MacCatalyst=70 }"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="100"/>
                                    <ColumnDefinition Width="100"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Frame  Grid.RowSpan="4" BorderColor="Transparent" Grid.Row="0" Grid.Column="0"  Padding="0" Margin="0,0,0,7">
                                    <Image  Source="emp_01.png"/>
                                </Frame>
                                <Label Text="Position" Grid.Column="1" Grid.Row="0" Margin="6,0,0,0"/>
                                <Label Text="Chairman" Grid.Row="0" Grid.Column="2"/>
                                <Label Text="Organization " Grid.Row="1" Grid.Column="1" Margin="6,0,0,0"/>
                                <Label Text="ABC Inc." Grid.Row="1" Grid.Column="2"/>
                                <Label Text="Date Of Birth " Grid.Row="2" Grid.Column="1" Margin="6,0,0,0"/>
                                <Label Text="09/17/1973" Grid.Row="2" Grid.Column="2"/>
                                <Label Text="Location " Grid.Row="3" Grid.Column="1" Margin="6,0,0,0"/>
                                <Label Text="Boston" Grid.Row="3" Grid.Column="2"/>
                                <Label Padding="0,10,0,10" Grid.Row="4" Grid.ColumnSpan="3"  LineBreakMode="WordWrap"  
                                            FontSize="14" CharacterSpacing="0.25" VerticalTextAlignment="Center" 
                                                Text="Robin Rane, Chairman of ABC Inc., leads with dedication and vision.Under his guidance, the company thrives and continues to make a significant impact in the industry.">
                                </Label>
                                <StackLayout Grid.Row="5" Orientation="Horizontal" Margin="0,0,0,12">
                                    <Label Text="&#xe700;" FontSize="16" Margin="0,2,2,2"
                                                   FontFamily='{OnPlatform Android=AccordionFontIcons.ttf#,WinUI=AccordionFontIcons.ttf#AccordionFontIcons,MacCatalyst=AccordionFontIcons,iOS=AccordionFontIcons}'
                                                   VerticalOptions="Center" VerticalTextAlignment="Center"/>
                                    <Label Text="(617) 555-1234" Grid.Column="1" VerticalOptions="Center" CharacterSpacing="0.25" FontSize="14"/>
                                </StackLayout>
                            </Grid>
                        </Grid>
                    </syncfusion:AccordionItem.Content>
                </syncfusion:AccordionItem>
                ..........................
            </syncfusion:SfAccordion.Items>
        </syncfusion:SfAccordion>
    </ContentPage.Content>
</ContentPage>

Now, run the application to render the following output.

.NET MAUI Accordion with items

You can download accordion sample for .NET MAUI here.

NOTE

When adding the template control inside the StackLayout or Grid with a height set to Auto, the child element will not receive the height changes at runtime. Since the SfAccordion is a template-based control, the default height value cannot be determined. Therefore, it is recommended to provide the HorizontalOptions and VerticalOptions as FillAndExpand options for the control.

Animation duration

The SfAccordion allows you to customize the expanding and collapsing duration of accordion items by using the AnimationDuration property. By default, the animation duration is set to 200 milliseconds.

<syncfusion:SfAccordion x:Name="accordion" 
                            AnimationDuration="150" />
accordion.AnimationDuration = 150;

Animation easing

You can customize the rate of change of a parameter over time or the animation style of an accordion item by using the AnimationEasing property. By default, the animation easing is set to Linear.

<syncfusion:SfAccordion x:Name="accordion"
                                AnimationEasing="SinOut" />
accordion.AnimationEasing = ExpanderAnimationEasing.SinOut;

Auto scroll position

The SfAccordion allows you to customize the scroll position of the expanded accordion item using the AutoScrollPosition property. By default, the auto-scroll position is set to MakeVisible.

<syncfusion:SfAccordion x:Name="accordion"
                             AutoScrollPosition="Top"/>
accordion.AutoScrollPosition = AccordionAutoScrollPosition.Top;

Bring an accordion item into view

The BringIntoView method is used to bring a specific item into view by scrolling to it programmatically.

<syncfusion:SfAccordion x:Name="accordion">
    <syncfusion:SfAccordion.Items>
        <syncfusion:AccordionItem>
            ...
            ...
        </syncfusion:AccordionItem>
    </syncfusion:SfAccordion.Items>
 </syncfusion:SfAccordion>
private void Button_Clicked(object sender, EventArgs e)
{
    accordion.BringIntoView(accordion.Items[15]);
}

Expand mode

You can expand single or multiple items using the ExpandMode property. By default, the expanded mode is set to Single.

<syncfusion:SfAccordion x:Name="accordion" 
                            ExpandMode="Multiple" />
accordion.ExpandMode = AccordionExpandMode.Multiple;

Item spacing

The SfAccordion allows you to customize the vertical spacing between the accordion items by using the ItemSpacing property.

<syncfusion:SfAccordion x:Name="accordion" 
                            ItemSpacing="6.0d" />
accordion.ItemSpacing = 6.0d;

NOTE

You can refer to our .NET MAUI Accordion feature tour page for its groundbreaking feature representations. You can also explore our .NET MAUI Accordion Example that shows you how to render and configure the Accordion in .NET MAUI.