Accordion

in namespace DotVVM.Bootstrap5.Controls

Wraps a group of AccordionItem controls to create an accordion-like behavior.

Usage & Scenarios

Wraps a group of AccordionItem controls to create an accordion-like behavior. When you expand one of the items inside, the others will collapse. Only one item can be expanded at the same time.

https://getbootstrap.com/docs/5.2/components/accordion/

Sample 1: ExpandedItemIndex

To expand specific AccordioItem use property ExpandedItemIndex with index of chosen item. If index is set to -1 or number higher than number of items, than no item will be expanded.

<bs:Accordion ExpandedItemIndex="{value: 2}">
    <bs:AccordionItem HeaderText="Header1" ContentText="Text1"/>
    <bs:AccordionItem HeaderText="Header2" ContentText="Text2"/>
    <bs:AccordionItem HeaderText="Header3" ContentText="Text3"/>
</bs:Accordion>
<bs:Accordion ExpandedItemIndex="{value: Index}">
    <bs:AccordionItem HeaderText="Header1" ContentText="Text1"/>
    <bs:AccordionItem HeaderText="Header2" ContentText="Text2"/>
    <bs:AccordionItem HeaderText="Header3" ContentText="Text3"/>
</bs:Accordion>

<dot:Button Click="{staticCommand: Index = 0}" Text="Change Index to 0" />
<dot:Button Click="{staticCommand: Index = 3}" Text="Change Index to 3" />
public class ViewModel : DotvvmViewModelBase
{
    public int Index { get; set; } = 2;
}

Sample 2: Data-binding the Accordion

The ExpandedItemIndex property specifies the index of the accordion item which is selected.

The DataSource property can be used to generate the items from an IEnumerable in the viewmodel.

Then you can use the following properties which tell the control what property of the collection item will be used:

  • ItemContentText specifies the property of the collection elements to be used as the content of the accordion item.
  • ItemHeaderText specifies the text in the accordion item header.
<bs:Accordion   DataSource="{value: Accordions}"
                ItemHeaderText="{value: Header}"
                ItemContentText="{value: Text}"
                ExpandedItemIndex="{value: IndexAdvanced}" />

<dot:Button Click="{command: AddItem()}" Text="Add" />
public class ViewModel : DotvvmViewModelBase
{
    public int IndexAdvanced { get; set; } = 1;
    public List<AccordionModel> Accordions { get; set; }
    public ViewModel()
    {
        Accordions = new List<AccordionModel>()
        {
            new AccordionModel()
            {
                Header = "Item 1",
                Text = "Text 1"
            },
            new AccordionModel()
            {
                Header = "Item 2",
                Text = "Text 2"
            }
        };
    }
    public void AddItem()
    {
        var itemNumber = Accordions.Count + 1;
        Accordions.Add(
            new AccordionModel()
            {
                Header = $"Item {itemNumber}",
                Text = $"Text  {itemNumber}"
            });
        IndexAdvanced = Accordions.Count - 1;
    }

    public class AccordionModel
    {
        public string Header { get; set; }
        public string Text { get; set; }
    }
}

Properties

Name Type Description Notes Default Value
property icon DataSource IValueBinding<IEnumerable<Object>> Gets or sets a data-source object from which the child controls will be generated.
attribute
inner element
static value
bindable
default
null
property icon ExpandedItemIndex IValueBinding<Int32> Gets or sets an index of expanded accordion item. If none of the accordion items are expanded, the index is set to -1.
attribute
inner element
static value
bindable
default
null
property icon ItemContentTemplate ITemplate Gets or sets a custom template for accordion item content when DataSource is set.
attribute
inner element
static value
bindable
default
null
property icon ItemContentText String Gets or sets a plain text for accordion item content when DataSource is set.
attribute
inner element
static value
bindable
default
null
property icon ItemHeaderTemplate ITemplate Gets or sets a custom template for accordion item header when DataSource is set.
attribute
inner element
static value
bindable
default
null
property icon ItemHeaderText String Gets or sets a plain text for accordion item header when DataSource is set.
attribute
inner element
static value
bindable
default
null
property icon Items List<IAccordionItem> Gets or sets the items inside the control.
attribute
inner element
static value
bindable
default
null
property icon Visible Boolean Gets or sets whether the control is visible. When set to false, `style="display: none"` will be added to this control.
attribute
inner element
static value
bindable
default
True

HTML produced by the control