Articles in this section
Category / Section

How to show or hide AccordionItem in Xamarin.Forms (SfAccordion)

1 min read

You can show or hide the AccoridonItem in Xamarin.Forms SfAccordion by handling the ViewModel bound collection with add or remove action.

C#

OnHideOrShow handler is used to show or hide the particular AccordionItem by updating the collection.

public class BehaviorClass : Behavior<ContentPage>
{
    Contact deletedItem = null;
    SfAccordion accordion;
    Button HideOrShow;
    protected override void OnAttachedTo(ContentPage bindable)
    {
        base.OnAttachedTo(bindable);
        accordion = bindable.FindByName<SfAccordion>("Accordion");
        HideOrShow = bindable.FindByName<Button>("HideOrShow");
        HideOrShow.Clicked += OnHideOrShow;
    }
    private void OnHideOrShow(object sender, EventArgs e)
    {
        var items = (sender as Button).BindingContext as ViewModel;
 
        if (deletedItem == null)
        {
            deletedItem = items.ContactsInfo[2];
            items.ContactsInfo.RemoveAt(2);
        }
        else
        {
            items.ContactsInfo.Insert(2, deletedItem);
            deletedItem = null;
        }
    }
}

Output

A screenshot of a cell phone

Description automatically generated

View sample in GitHub

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied