一、拖放控件
二、创建数据源绑定对象
/// <summary> /// 操作记录类型 /// </summary> public class LogTypeInfo { /// <summary> /// ID /// </summary> public int Id { get; set; } /// <summary> /// 父节点ID /// </summary> public int ParentId { get; set; } /// <summary> /// 名称 /// </summary> public string LogName { get; set; } }
三、绑定对象的值
/// <summary> /// 操作记录 /// </summary> public static readonly List<string> lstLogType = new List<string>() { "所有日志","登录系统","退出系统","新增房源","修改房源","删除房源","激活房源", "资料房源","发布房源","打印房源","查看业主","资料业主","新增客源", };
以上都是准备工作。
开始作业,假设以“所有日志”作为父级,其它的作为子级
//实例化数据源列表 List<LogTypeInfo> lstTree = new List<LogTypeInfo>(); //数据内容 List<string> lstLogType = lstLogType; //给数据列表赋值 for (int i = 0; i < lstLogType.Count; i++) { LogTypeInfo model = new LogTypeInfo(); model.Id = i; model.LogName = lstLogType[i]; //设置父子级别 if (i > 0) { model.ParentId = 0; } lstTree.Add(model); } //绑定父子级字段 trLog.KeyFieldName = "Id"; trLog.ParentFieldName = "ParentId"; trLog.DataSource = lstTree; //展开节点 trLog.ExpandAll();
结果如图
其中字体颜色可以通过CustomDrawNodeCell事件修改颜色
private void trLog_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e) { if (trLog.DataSource != null) { switch (e.CellValue.ToString()) { case "登录系统": e.Appearance.ForeColor = StaticHelper.gColorTextG; break; case "退出系统": e.Appearance.ForeColor = StaticHelper.gColorTextC; break; case "新增房源": e.Appearance.ForeColor = StaticHelper.gColorTextR; break; case "新增客源": e.Appearance.ForeColor = StaticHelper.gColorTextR; break; case "自动转房客": e.Appearance.ForeColor = StaticHelper.gColorTextR; break; case "合同记录": e.Appearance.ForeColor = StaticHelper.gColorTextR; break; case "新增员工": e.Appearance.ForeColor = StaticHelper.gColorTextR; break; } } }