• 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>

    运行程序

  • 相关阅读:
    绿联 安装memcached容器 - 一个开源的高性能分布式内存对象缓存系统
    速码!!BGP最全学习笔记:路由反射器实验配置
    C#中的日期时间比较和格式化的方法
    vue3 弹窗开发之三,完善版
    第十章 路由器的基本配置
    Dynamsoft Dynamic .NET TWAIN for net Crack
    Go语法实现分析之chan、go func、类型转换
    2021 RoboCom 世界机器人开发者大赛-本科组(决赛)
    Fortify-设置中文语言
    网络安全之Windows提权(上篇)(高级进阶)
  • 原文地址:https://blog.csdn.net/loongsking/article/details/134454520