WPF Accordion Control - Bind to a Data Source that Contains Hierarchical Data Objects of Different Types
This example binds the WPF AccordionControl
to a hierarchical data structure that includes objects of different types. Use this technique when your data model does not expose a unified child collection (for instance, when parent and child objects do not share a common base class).
The implementation leverages the ChildrenSelector
property to retrieve child items at runtime. The standard ChildrenPath
property cannot traverse mixed-type hierarchies. The ChildrenSelector
property delegates child resolution to your custom logic.
The AccordionControl
binds to a flat list of Category
objects. Each Category
contains a collection of Item
objects in its Items
property.
public class Category {
public string CategoryName { get; set; }
public ObservableCollection<Item> Items { get; set; }
}
public class Item {
public string ItemName { get; set; }
}
The custom selector (MySelector
) implements the IChildrenSelector
interface. The selector returns child items only for Category
objects:
public class MySelector : IChildrenSelector {
public IEnumerable SelectChildren(object item) {
if (item is Category category)
return category.Items;
return null;
}
}
Register the selector as a resource and assign it to the AccordionControl.ChildrenSelector
property:
<local:MySelector x:Key="mySelector" />
<dxa:AccordionControl
ItemsSource="{Binding MyData.Categories}"
ChildrenSelector="{StaticResource mySelector}" />
Define templates to display Category
and Item
objects:
<DataTemplate DataType="{x:Type local:Category}">
<TextBlock Text="{Binding CategoryName}" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:Item}">
<TextBlock Text="{Binding ItemName}" />
</DataTemplate>
- MainWindow.xaml (VB: MainWindow.xaml)
- ViewModel.cs (VB: ViewModel.vb)
- AccordionControl
- AccordionItem
- AccordionViewMode
- AccordionControl.ChildrenSelector
- Data Binding
- MVVM Framework
- WPF Accordion Control - Bind to Hierarchical Data Structure
- WPF MVVM Framework - Use View Models Generated at Compile Time
- WPF Dock Layout Manager - Bind the View Model Collection with LayoutAdapters
- WPF Dock Layout Manager - Bind the View Model Collection with IMVVMDockingProperties
- WPF Dock Layout Manager - Populate a DockLayoutManager LayoutGroup with the ViewModels Collection
(you will be redirected to DevExpress.com to submit your response)