当前位置: 首页 > article >正文

wpf devexpress添加TreeListControl到项目

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

添加数据模型

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

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

Parent字段包含父索引节点

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

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

添加视图模型

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

using DevExpress.Mvvm;

namespace DxTreeListGettingStarted {
    public partial class MainWindow : Window { ... }
    public class Employee { ... }
    public static class Staff { ... }

    public class MainWindowViewModel : ViewModelBase {
        public MainWindowViewModel() {
            Employees = Staff.GetStaff();
        }
        public List<Employee> Employees { get; private set; }
    }
}

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

<Window
    ... >
    <Window.DataContext>
        <local:MainWindowViewModel/>
    </Window.DataContext>

添加TreeListControl到视图

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

选择TreeListControl点击Quick Actions

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

选择Employees在对话框点击OK

设置AutoGenerateColumns添加AddNew

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

<dxg:TreeListControl AutoGenerateColumns="AddNew" 
                        ItemsSource="{Binding Employees}">
    <dxg:TreeListControl.View>
        <dxg:TreeListView />
    </dxg:TreeListControl.View>
</dxg:TreeListControl>

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

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

<dxg:TreeListControl ItemsSource="{Binding Employees}"
        AutoGenerateColumns="AddNew" >
    <dxg:TreeListControl.View>
        <dxg:TreeListView KeyFieldName="ID" ParentFieldName="ParentID"
                            AutoWidth="True" AutoExpandAllNodes="True" />
    </dxg:TreeListControl.View>
</dxg:TreeListControl>

运行程序


http://www.kler.cn/a/132436.html

相关文章:

  • RHCE——系统的延迟任务及定时任务
  • More Effective C++ Item 7:区别使用()和{}创建对象
  • 网络安全:我们的安全防线
  • 【嵌入式】关于push老仓库到新仓库的方法
  • 【ACM独立出版|高校主办】第四届信号处理与通信技术国际学术会议(SPCT 2024)
  • 【分割评价指标-nnUNet V2训练】- AutoDL
  • 腾讯云轻量级服务器和云服务器什么区别?轻量服务器是干什么用的
  • js 给选中的文字添加颜色、替换文字内容...,选中状态去除后更改还在(document.execCommand)
  • 3D建模基础教程:可编辑多边形建模的基础认识
  • 2023年亚太杯数学建模思路 - 案例:异常检测
  • Unity——URP相机详解
  • Flutter笔记: 在Flutter应用中使用SQLite数据库
  • FPGA时序约束(七)文献时序约束实验测试
  • nginx的所有知识点以及使用
  • vue3中祖孙组件之间的通信provide和inject
  • 【LeetCode】 第 371 场周赛
  • py split 用法
  • Unity减少发布打包文件的体积(二)——设置WebGL发布时每张图片的压缩方式
  • 【STM32】DMA(直接存储器访问)
  • IDEA中安装Docker插件实现远程访问Docker
  • 【Spring篇】使用注解进行开发
  • TensorFlow:GPU的使用
  • redis+python 提取免费代理ip/验证/留接口
  • TensorFlow C++编译及推理
  • 生活总是自己的,请尽情打扮,尽情可爱,,
  • 【2023春李宏毅机器学习】快速了解机器学习基本原理