Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

How to: Bind the AccordionControl to Data Using the HierarchicalData Template

  • 3 minutes to read

This example demonstrates how to bind the AccordionControl to a data using System.Windows.HierarchicalDataTemplate objects.

The AccordionControl is bound to a data source. A panel on the right contains the dedicated Expand and Collapse buttons and allows editing a description of the currently selected item.

The image below shows the result:

AccordionControl HierarchicalDataTemplate

Refer to the Data Binding topic to learn more.

View Example

using System.Collections.Generic;

namespace HierarchicalDataTemplate {

    public class ViewModel {
        public Data MyData { get; set; }
        public object SelectedItem { get; set; }
        public ViewModel() {
            MyData = new Data();
        }
    }
    public class Data {
        public ObservableCollection<Category> Categories { get; set; }
        public Data() {
            Categories = new ObservableCollection<Category>();
            ObservableCollection<Item> subitems = new ObservableCollection<Item>();
            subitems.Add(new Item() { ItemName = "Chair", Description = "A red chair." });
            subitems.Add(new Item() { ItemName = "Table", Description = "An old table." });
            Categories.Add(new Category() { CategoryName = "Furniture", Items = subitems });
            ObservableCollection<Item> books = new ObservableCollection<Item>();
            books.Add(new Item() { ItemName = "Dictionary", Description = "My old French-English Dictionary" });
            Categories.Add(new Category() { CategoryName = "Books", Items = books });
        }
    }
    public class Category {
        public string CategoryName { get; set; }
        public string Description { get; set; }
        public List<Item> Items { get; set; }
    }
    public class Item {
        public string ItemName { get; set; }
        public string Description { get; set; }
    }
}