We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to expand or collapse Accordion programmatically in Xamarin.Forms (SfAccordion)


You can programmatically expand or collapse the AccordionItem in Xamarin.Forms SfAccordion by binding the model property to AccordionItem.IsExpanded property.

 

XAML

IsExpanded model property is bound to IsExpanded property of AccordionItem to expand or collapse when update property value programmatically.

 

<DataTemplate>
    <syncfusion:AccordionItem IsExpanded="{Binding IsExpand}">
        <syncfusion:AccordionItem.Header>
            <Grid HeightRequest="60">
                <Label Text="{Binding Name}" FontSize="Medium"/>
            </Grid>
        </syncfusion:AccordionItem.Header>
        <syncfusion:AccordionItem.Content>
            <Grid>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="50"/>
                        <RowDefinition Height="50"/>
                    </Grid.RowDefinitions>
                    <Label Text="{Binding Description}"/>
                    <Button x:Name="btn" Grid.Row="1" Text="{Binding Name}" Command="{Binding Path=BindingContext.ButtonCommand, Source={x:Reference Accordion}}" CommandParameter="{x:Reference btn}"/>
                </Grid>
            </Grid>
        </syncfusion:AccordionItem.Content>
    </syncfusion:AccordionItem>
</DataTemplate>

 

C#

OnExpandCollapse is used to change the IsExpand property to expand or collapse the item.

 

public class ItemInfoRepository
{
    public Command<object> ExpandCollapse{ get; set; }
 
    public ItemInfoRepository()
    {
        ExpandCollapse= new Command<object>(OnExpandCollapse);
    }
    
    private void OnExpandCollapse (object obj)
    {
        var item = (obj as Button).BindingContext as ItemInfo;
        item.IsExpand = !item.IsExpand;
    }
}

 

View sample in GitHub

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile