How to expand or collapse accordion item programmatically in .NET MAUI Accordion (SfAccordion)?
To expand or collapse an accordion item programmatically in .NET MAUI Accordion, you can bind the AccordionItem.IsExpanded
property of the AccordionItem
to a model property. This allows dynamic control of the accordion’s state using commands.
Bind the IsExpanded property of AccordionItem to a model property such as IsExpand:
XAML
<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="*"/>
<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>
Implementing the Command to Toggle Expansion
In your view model or code-behind, implement a command that toggles the IsExpand
property of your model. This command can be bound to a button or invoked from other event handlers.
C#
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;
}
}
Download the complete sample on GitHub.
Conclusion
I hope you enjoyed learning how to programmatically expand or collapse items in .NET MAUI Accordion.
You can refer to our .NET MAUI Accordion feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications.
For current customers, check out our components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!