欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

DevExpress的TreeList怎样给树节点设置图标

程序员文章站 2024-01-20 10:51:28
场景 DevExpress的TreeList怎样设置数据源使其显示成单列树形结构: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102742426 在上面设置TreeList的数据源并设置其为树形结构后,如何给树形结构设置图标 ......

场景

devexpress的treelist怎样设置数据源使其显示成单列树形结构:

https://blog.csdn.net/badao_liumang_qizhi/article/details/102742426

在上面设置treelist的数据源并设置其为树形结构后,如何给树形结构设置图标

DevExpress的TreeList怎样给树节点设置图标

 

 

DevExpress的TreeList怎样给树节点设置图标

注:

博客主页:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

首先按照上面搭建好树形结构和设置数据源之后效果如下

DevExpress的TreeList怎样给树节点设置图标

 

 

然后在项目下新建resources目录,将图标文件复制进去

DevExpress的TreeList怎样给树节点设置图标

然后在窗体类中获取图标数组

 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的目录如此

DevExpress的TreeList怎样给树节点设置图标

 

 

 

然后在窗体加载完的事件中对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大量编程资源";


            list<datatreenode> data = new list<datatreenode>();

            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;
        }
    }
}