添加一个数据模型
AccordionControl可以被束缚到任何实现IEnumerable接口的对象或者它的派生类(例如IList,ICollection)
如下代码例子示范了一个简单的数据模型使用:
using System.Collections.Generic;namespace DxAccordionGettingStarted {public class Employee {public int ID { get; set; }public string Name { get; set; }public string Position { get; set; }public string Department { get; set; }public override string ToString() {return Name;}}public static class Staff {public static List<Employee> GetStaff() {List<Employee> employees = new List<Employee>();employees.Add(new Employee() { ID = 1, Name = "Gregory S. Price", Department = "Management", Position = "President" });employees.Add(new Employee() { ID = 2, Name = "Irma R. Marshall", Department = "Marketing", Position = "Vice President" });employees.Add(new Employee() { ID = 3, Name = "John C. Powell", Department = "Operations", Position = "Vice President" });employees.Add(new Employee() { ID = 4, Name = "Christian P. Laclair", Department = "Production", Position = "Vice President" });employees.Add(new Employee() { ID = 5, Name = "Karen J. Kelly", Department = "Finance", Position = "Vice President" });employees.Add(new Employee() { ID = 6, Name = "Brian C. Cowling", Department = "Marketing", Position = "Manager" });employees.Add(new Employee() { ID = 7, Name = "Thomas C. Dawson", Department = "Marketing", Position = "Manager" });employees.Add(new Employee() { ID = 8, Name = "Angel M. Wilson", Department = "Marketing", Position = "Manager" });employees.Add(new Employee() { ID = 9, Name = "Bryan R. Henderson", Department = "Marketing", Position = "Manager" });employees.Add(new Employee() { ID = 10, Name = "Harold S. Brandes", Department = "Operations", Position = "Manager" });employees.Add(new Employee() { ID = 11, Name = "Michael S. Blevins", Department = "Operations", Position = "Manager" });employees.Add(new Employee() { ID = 12, Name = "Jan K. Sisk", Department = "Operations", Position = "Manager" });employees.Add(new Employee() { ID = 13, Name = "Sidney L. Holder", Department = "Operations", Position = "Manager" });employees.Add(new Employee() { ID = 14, Name = "James L. Kelsey", Department = "Production", Position = "Manager" });employees.Add(new Employee() { ID = 15, Name = "Howard M. Carpenter", Department = "Production", Position = "Manager" });employees.Add(new Employee() { ID = 16, Name = "Jennifer T. Tapia", Department = "Production", Position = "Manager" });employees.Add(new Employee() { ID = 17, Name = "Judith P. Underhill", Department = "Finance", Position = "Manager" });employees.Add(new Employee() { ID = 18, Name = "Russell E. Belton", Department = "Finance", Position = "Manager" });return employees;}}
}
添加视图模型
创建视图模型从数据模型中调用数据:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;namespace DxAccordionGettingStarted {public class MainWindowViewModel {public MainWindowViewModel() {var employeeDepartments = Staff.GetStaff().GroupBy(x => x.Department).Select(x => new EmployeeDepartment(x.Key, x.ToArray()));EmployeeDepartments = new ObservableCollection<EmployeeDepartment>(employeeDepartments.ToArray());}public ObservableCollection<EmployeeDepartment> EmployeeDepartments { get; set; }}public class EmployeeDepartment {public string Name { get; set; }public ObservableCollection<Employee> Employees { get; set; }public EmployeeDepartment(string name, IEnumerable<Employee> employees) {Name = name;Employees = new ObservableCollection<Employee>(employees);}public override string ToString() {return Name;}}
}
生成解决方案。调用MainWindow Quick Actions和定义窗口数据上下文作为如下图片显示:
添加AccordionControl到视图
拖动AccordionControl从DX.23.1:Navigation & Layout工具箱页面,拖动到MainWindow:
右键点击控件选择Layout | Reset All去允许AccordionControl填充整个窗口:
绑定AccordionControl到数据
调用AccordinControl Quick Action和定义ItemsSource字段:
定义ChildrenPath字段指定路径到属性包含一个accordion item子对象:
代码例子如下示范了生成的代码:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxa="http://schemas.devexpress.com/winfx/2008/xaml/accordion" xmlns:local="clr-namespace:DxAccordionGettingStarted" x:Class="DxAccordionGettingStarted.MainWindow" Title="MainWindow" Height="350" Width="525"><Window.DataContext><local:MainWindowViewModel/></Window.DataContext><Grid><dxa:AccordionControl ItemsSource="{Binding EmployeeDepartments}" ChildrenPath="Employees"/></Grid>
</Window>
获得结果
运行解决方案显示如下: