• wpf devexpress添加TreeListControl到项目


    此教程示范如何添加TreeListControl到项目和绑定控件自引用数据源

    添加数据模型

    绑定tree,并添加如下字段到数据源对象:

    Key字段包含唯一值索引节点

    Parent字段包含父索引节点

    添加数据模型(Employee和Staff类)到MainWindow.xaml.cs 文件:

    1. namespace DxTreeListGettingStarted {
    2. public partial class MainWindow : Window { ... }
    3. public class Employee {
    4. public int ID { get; set; }
    5. public int ParentID { get; set; }
    6. public string Name { get; set; }
    7. public string Position { get; set; }
    8. public string Department { get; set; }
    9. }
    10. public static class Staff {
    11. public static List GetStaff() {
    12. List staff = new List();
    13. staff.Add(new Employee() { ID = 0, Name = "Gregory S. Price", Department = "", Position = "President" });
    14. staff.Add(new Employee() { ID = 1, ParentID = 0, Name = "Irma R. Marshall", Department = "Marketing", Position = "Vice President" });
    15. staff.Add(new Employee() { ID = 2, ParentID = 0, Name = "John C. Powell", Department = "Operations", Position = "Vice President" });
    16. staff.Add(new Employee() { ID = 3, ParentID = 0, Name = "Christian P. Laclair", Department = "Production", Position = "Vice President" });
    17. staff.Add(new Employee() { ID = 4, ParentID = 0, Name = "Karen J. Kelly", Department = "Finance", Position = "Vice President" });
    18. staff.Add(new Employee() { ID = 5, ParentID = 1, Name = "Brian C. Cowling", Department = "Marketing", Position = "Manager" });
    19. staff.Add(new Employee() { ID = 6, ParentID = 1, Name = "Thomas C. Dawson", Department = "Marketing", Position = "Manager" });
    20. staff.Add(new Employee() { ID = 7, ParentID = 1, Name = "Angel M. Wilson", Department = "Marketing", Position = "Manager" });
    21. staff.Add(new Employee() { ID = 8, ParentID = 1, Name = "Bryan R. Henderson", Department = "Marketing", Position = "Manager" });
    22. staff.Add(new Employee() { ID = 9, ParentID = 2, Name = "Harold S. Brandes", Department = "Operations", Position = "Manager" });
    23. staff.Add(new Employee() { ID = 10, ParentID = 2, Name = "Michael S. Blevins", Department = "Operations", Position = "Manager" });
    24. staff.Add(new Employee() { ID = 11, ParentID = 2, Name = "Jan K. Sisk", Department = "Operations", Position = "Manager" });
    25. staff.Add(new Employee() { ID = 12, ParentID = 2, Name = "Sidney L. Holder", Department = "Operations", Position = "Manager" });
    26. staff.Add(new Employee() { ID = 13, ParentID = 3, Name = "James L. Kelsey", Department = "Production", Position = "Manager" });
    27. staff.Add(new Employee() { ID = 14, ParentID = 3, Name = "Howard M. Carpenter", Department = "Production", Position = "Manager" });
    28. staff.Add(new Employee() { ID = 15, ParentID = 3, Name = "Jennifer T. Tapia", Department = "Production", Position = "Manager" });
    29. staff.Add(new Employee() { ID = 16, ParentID = 4, Name = "Judith P. Underhill", Department = "Finance", Position = "Manager" });
    30. staff.Add(new Employee() { ID = 17, ParentID = 4, Name = "Russell E. Belton", Department = "Finance", Position = "Manager" });
    31. return staff;
    32. }
    33. }
    34. }

    添加视图模型

    创建视图模型暴露Employees字段:

    1. using DevExpress.Mvvm;
    2. namespace DxTreeListGettingStarted {
    3. public partial class MainWindow : Window { ... }
    4. public class Employee { ... }
    5. public static class Staff { ... }
    6. public class MainWindowViewModel : ViewModelBase {
    7. public MainWindowViewModel() {
    8. Employees = Staff.GetStaff();
    9. }
    10. public List Employees { get; private set; }
    11. }
    12. }

    设置window数据上下文到视图模型:

    1. ... >

    添加TreeListControl到视图

    拖动TreeListControl组件从工具箱到form里面

    选择TreeListControl点击Quick Actions

    点击重新计算按钮到ItemsSource属性调用上下文菜单。选择创建数据绑定

    选择Employees在对话框点击OK

    设置AutoGenerateColumns添加AddNew

    切换到XAML视图,定义TreeListControl 视图:

    1. <dxg:TreeListControl AutoGenerateColumns="AddNew"
    2. ItemsSource="{Binding Employees}">
    3. <dxg:TreeListControl.View>
    4. <dxg:TreeListView />
    5. dxg:TreeListControl.View>
    6. dxg:TreeListControl>

    返回设计器视图,选择TreeListView调用Quick Actions

    启动AutoWidth和AutoExpandAllNodes选项。指定服务行(TreeListView.KeyFieldName 和 TreeListView.ParentFieldName)生成tree:

    1. <dxg:TreeListControl ItemsSource="{Binding Employees}"
    2. AutoGenerateColumns="AddNew" >
    3. <dxg:TreeListControl.View>
    4. <dxg:TreeListView KeyFieldName="ID" ParentFieldName="ParentID"
    5. AutoWidth="True" AutoExpandAllNodes="True" />
    6. dxg:TreeListControl.View>
    7. dxg:TreeListControl>

    运行程序

  • 相关阅读:
    【无标题】Linux服务器上监控网络带宽的18个常用命令
    【LVGL】ANIM(动画)时间线学习
    Git 的基础命令 码云 gitee
    【最新版】ChatGPT/GPT4科研应用与AI绘图论文写作(最新增加Claude3、Gemini、Sora、GPTs技术及AI领域中的集中大模型的最新技术)
    app.json文件内容错误
    SpringCloud | 单体商城项目拆分(微服务)
    怎样做好金融投资翻译
    float型和double型的区别
    Direct3D的初始化
    Linux系统下建立Socket聊天服务器
  • 原文地址:https://blog.csdn.net/loongsking/article/details/134454520