场景
DevExpress的TreeList怎样设置数据源使其显示成单列树形结构:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102742426
在上面设置TreeList的数据源并设置其为树形结构后,如何给树形结构设置图标
注:
博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
首先按照上面搭建好树形结构和设置数据源之后效果如下
然后在项目下新建Resources目录,将图标文件复制进去
然后在窗体类中获取图标数组
public static System.Drawing.Image[] imgs = new System.Drawing.Image[] { System.Drawing.Image.FromFile(@"../../Resources/a.png"), System.Drawing.Image.FromFile(@"../../Resources/b.png"), System.Drawing.Image.FromFile(@"../../Resources/c.png") };
这里路径为什么是两层上级目录,因为Resources目录相对应bin/debug的目录如此
然后在窗体加载完的事件中对TreeList设置完数据源之后,设置每个节点的图标。
System.Windows.Forms.ImageList imgList = new System.Windows.Forms.ImageList(); imgList.Images.AddRange(imgs); treeList1.SelectImageList = imgList; treeList1.Nodes.FirstOrDefault().ImageIndex = 0; treeList1.Nodes.FirstOrDefault().FirstNode.ImageIndex = 1; treeList1.Nodes.FirstOrDefault().FirstNode.FirstNode.ImageIndex = 2;
这里是通过treeList1.Nodes.FirstOrDefault()找到第一个节点或者默认节点,即根节点。
然后通过FirstNode找到第一个子节点。
然后treeList1.SelectImageList = imgList设置其图标源是一个图标数组。
然后指定每个节点的ImageIndex 就是在图标数组中的索引。
完整示例代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace TreeListTest { public partial class Form1 : Form { public static System.Drawing.Image[] imgs = new System.Drawing.Image[] { System.Drawing.Image.FromFile(@"../../Resources/a.png"), System.Drawing.Image.FromFile(@"../../Resources/b.png"), System.Drawing.Image.FromFile(@"../../Resources/c.png") }; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { #region 设置列头、节点指示器面板、表格线样式 treeList1.OptionsView.ShowColumns = false; //隐藏列标头 treeList1.OptionsView.ShowIndicator = false; //隐藏节点指示器面板 treeList1.OptionsView.ShowHorzLines = false; //隐藏水平表格线 treeList1.OptionsView.ShowVertLines = false; //隐藏垂直表格线 treeList1.OptionsView.ShowIndentAsRowStyle = false; #endregion #region 初始禁用单元格选中,禁用整行选中 treeList1.OptionsView.ShowFocusedFrame = true; //设置显示焦点框 treeList1.OptionsSelection.EnableAppearanceFocusedCell = false; //禁用单元格选中 treeList1.OptionsSelection.EnableAppearanceFocusedRow = false; //禁用正行选中 #endregion #region 设置TreeList的展开折叠按钮样式和树线样式 treeList1.OptionsView.ShowButtons = true; //显示展开折叠按钮 treeList1.LookAndFeel.UseDefaultLookAndFeel = false; //禁用默认外观与感觉 treeList1.LookAndFeel.UseWindowsXPTheme = true; //使用WindowsXP主题 treeList1.TreeLineStyle = DevExpress.XtraTreeList.LineStyle.Percent50; //设置树线的样式 #endregion #region 添加单列 DevExpress.XtraTreeList.Columns.TreeListColumn colNode = new DevExpress.XtraTreeList.Columns.TreeListColumn(); colNode.Name = String.Format("col{0}", "NodeText"); colNode.Caption = "NodeText"; colNode.FieldName = "NodeText"; colNode.VisibleIndex = 0; colNode.Visible = true; colNode.OptionsColumn.AllowEdit = false; //是否允许编辑 colNode.OptionsColumn.AllowMove = false; //是否允许移动 colNode.OptionsColumn.AllowMoveToCustomizationForm = false; //是否允许移动至自定义窗体 colNode.OptionsColumn.AllowSort = false; //是否允许排序 colNode.OptionsColumn.FixedWidth = false; //是否固定列宽 colNode.OptionsColumn.ReadOnly = true; //是否只读 colNode.OptionsColumn.ShowInCustomizationForm = true; //移除列后是否允许在自定义窗体中显示 treeList1.Columns.Clear(); treeList1.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] { colNode }); #endregion treeList1.KeyFieldName = "Id"; treeList1.ParentFieldName = "ParentId"; DataTreeNode node1 = new DataTreeNode(); node1.Id = "1"; node1.ParentId = null; node1.NodeText = "1公众号"; DataTreeNode node11 = new DataTreeNode(); node11.Id = "2"; node11.ParentId = "1"; node11.NodeText = "1-1霸道的程序猿"; DataTreeNode node111 = new DataTreeNode(); node111.Id = "3"; node111.ParentId = "2"; node111.NodeText = "1-1-1大量编程资源"; Listdata = new List (); data.Add(node1); data.Add(node11); data.Add(node111); treeList1.DataSource = data; treeList1.RefreshDataSource(); System.Windows.Forms.ImageList imgList = new System.Windows.Forms.ImageList(); imgList.Images.AddRange(imgs); treeList1.SelectImageList = imgList; treeList1.Nodes.FirstOrDefault().ImageIndex = 0; treeList1.Nodes.FirstOrDefault().FirstNode.ImageIndex = 1; treeList1.Nodes.FirstOrDefault().FirstNode.FirstNode.ImageIndex = 2; } } }