DevExpress的TreeList实现显示本地文件目录并自定义右键实现删除与重命名文件
程序员文章站
2023-11-18 13:06:40
场景 使用DevExpress的TreeList显示本磁盘下文件目录并在树节点上右键实现删除与添加文件。 效果 自定义右键效果 实现 首先在包含Treelist的窗体的load方法中对treelist进行初始化 Common.DataTreeListHelper.RefreshTreeData(th ......
场景
使用devexpress的treelist显示本磁盘下文件目录并在树节点上右键实现删除与添加文件。
效果
自定义右键效果
实现
首先在包含treelist的窗体的load方法中对treelist进行初始化
common.datatreelisthelper.refreshtreedata(this.treelist1, 2);
其中this.treelist1就是当前窗体的treelist对象
然后第二个参数是默认展开级别。
public static void refreshtreedata(devexpress.xtratreelist.treelist treelist, int expandtolevel) { string rootnodeid = common.global.appconfig.testdatadir; string rootnodetext = icsharpcode.core.stringparser.parse(resourceservice.getstring("pad_datatree_rootnodetext")); //"全部实验数据"; string fieldname = "nodetext"; string keyfieldname = "id"; string parentfieldname = "parentid"; list<datatreenode> data = new list<datatreenode>(); data = datatreelisthelper.parsedir(common.global.appconfig.testdatadir, data); data.add(new datatreenode() { id = rootnodeid, parentid = string.empty, nodetext = rootnodetext, nodetype = datatreenodetypes.folder }); datatreelisthelper.settreelistdatasource(treelist, data, fieldname, keyfieldname, parentfieldname); treelist.expandtolevel(expandtolevel); }
在上面方法中新建根节点,根节点的id就是要显示的目录,在配置文件中读取。
根节点的显示文本就是显示“全部实验数据”,从配置文件中获取。
然后调用工具类将目录结构转换成带父子级关系的节点的list,然后再将根节点添加到list。
然后调用设置treelist数据源的方法。
在上面方法中存取节点信息的datatreenode
public class datatreenode { private string id; private string parentid; private string nodetext; private string createdate; private string fullpath; private string taskfile; private string barcode; private datatreenodetypes nodetype = datatreenodetypes.folder; public string id { get { return id; } set { id = value; } } public string parentid { get { return parentid; } set { parentid = value; } } public string nodetext { get { return nodetext; } set { nodetext = value; } } public string createdate { get { return createdate; } set { createdate = value; } } public string fullpath { get { return fullpath; } set { fullpath = value; } } public string taskfile { get { return taskfile; } set { taskfile = value; } } public string barcode { get { return barcode; } set { barcode = value; } } public datatreenodetypes nodetype { get { return nodetype; } set { nodetype = value; } } }
在上面方法中将目录结构转换为节点list的方法
public static list<datatreenode> parsedir(string datarootdir, list<datatreenode> data) { if (data == null) { data = new list<datatreenode>(); } if (!system.io.directory.exists(datarootdir)) { return data; } datatreenode node = null; system.io.directoryinfo dir = new system.io.directoryinfo(datarootdir); system.io.directoryinfo[] subdirs = dir.getdirectories(); foreach(system.io.directoryinfo subdir in subdirs) { node = new datatreenode(); node.id = subdir.fullname; node.parentid = dir.fullname; node.nodetext = subdir.name; node.createdate = string.format("{0:yyyy-mm-dd hh:mm:ss}", subdir.creationtime); node.fullpath = subdir.fullname; node.taskfile = string.empty; //任务文件名 node.nodetype = datatreenodetypes.folder; data.add(node); parsedir(subdir.fullname, data); } system.io.fileinfo[] subfiles = dir.getfiles(); return data; }
通过递归将上面传递过来的目录下的结构构造成节点的list并返回。
通过解析实验目录的方法返回list后再调用刷新treelist节点的方法
settreelistdatasource
public static void settreelistdatasource(devexpress.xtratreelist.treelist treelist, list<datatreenode> data, string fieldname, string keyfieldname, string parentfieldname) { #region 设置节点图标 system.windows.forms.imagelist imglist = new system.windows.forms.imagelist(); imglist.images.addrange(imgs); treelist.selectimagelist = imglist; //目录展开 treelist.afterexpand -= treelist_afterexpand; treelist.afterexpand += treelist_afterexpand; //目录折叠 treelist.aftercollapse -= treelist_aftercollapse; treelist.aftercollapse += treelist_aftercollapse; //数据节点单击,开启整行选中 treelist.mouseclick -= treelist_mouseclick; treelist.mouseclick += treelist_mouseclick; //数据节点双击选中 treelist.mousedoubleclick -= treelist_mousedoubleclick; treelist.mousedoubleclick += treelist_mousedoubleclick; //焦点离开事件 treelist.lostfocus -= treelist_lostfocus; treelist.lostfocus += treelist_lostfocus; #endregion #region 设置列头、节点指示器面板、表格线样式 treelist.optionsview.showcolumns = false; //隐藏列标头 treelist.optionsview.showindicator = false; //隐藏节点指示器面板 treelist.optionsview.showhorzlines = false; //隐藏水平表格线 treelist.optionsview.showvertlines = false; //隐藏垂直表格线 treelist.optionsview.showindentasrowstyle = false; #endregion #region 初始禁用单元格选中,禁用整行选中 treelist.optionsview.showfocusedframe = true; //设置显示焦点框 treelist.optionsselection.enableappearancefocusedcell = false; //禁用单元格选中 treelist.optionsselection.enableappearancefocusedrow = false; //禁用正行选中 //treelist.appearance.focusedrow.backcolor = system.drawing.color.red; //设置焦点行背景色 #endregion #region 设置treelist的展开折叠按钮样式和树线样式 treelist.optionsview.showbuttons = true; //显示展开折叠按钮 treelist.lookandfeel.usedefaultlookandfeel = false; //禁用默认外观与感觉 treelist.lookandfeel.usewindowsxptheme = true; //使用windowsxp主题 treelist.treelinestyle = devexpress.xtratreelist.linestyle.percent50; //设置树线的样式 #endregion #region 添加单列 devexpress.xtratreelist.columns.treelistcolumn colnode = new devexpress.xtratreelist.columns.treelistcolumn(); colnode.name = string.format("col{0}", fieldname); colnode.caption = fieldname; colnode.fieldname = fieldname; 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; //移除列后是否允许在自定义窗体中显示 treelist.columns.clear(); treelist.columns.addrange(new devexpress.xtratreelist.columns.treelistcolumn[] { colnode }); #endregion #region 绑定数据源 treelist.datasource = null; treelist.keyfieldname = keyfieldname; treelist.parentfieldname = parentfieldname; treelist.datasource = data; treelist.refreshdatasource(); #endregion #region 初始化图标 setnodeimageindex(treelist.nodes.firstordefault()); #endregion }
如果不考虑根据文件还是文件夹设置节点图标和绑定其他双击事件等。
直接关注鼠标单击事件的绑定和下面初始化样式的设置。
在单击鼠标节点绑定的方法中
private static void treelist_mouseclick(object sender, system.windows.forms.mouseeventargs e) { devexpress.xtratreelist.treelist treelist = sender as devexpress.xtratreelist.treelist; if (treelist != null && treelist.selection.count == 1) { object idvalue = null; string stridvalue = string.empty; datatreenode nodedata = null; list<datatreenode> datasource = treelist.datasource as list<datatreenode>; if (datasource != null) { idvalue = treelist.selection[0].getvalue("id"); stridvalue = idvalue.tostring(); nodedata = datasource.where<datatreenode>(p => p.id == stridvalue).firstordefault<datatreenode>(); if (nodedata != null) { if (nodedata.nodetype == datatreenodetypes.file) { treelist.optionsselection.enableappearancefocusedrow = true; //启用整行选中 if (e.button == system.windows.forms.mousebuttons.right) { system.windows.forms.contextmenu ctxmenu = new system.windows.forms.contextmenu(); #region 右键弹出上下文菜单 - 删除数据文件 system.windows.forms.menuitem mnudelete = new system.windows.forms.menuitem(); mnudelete.text = "删除"; mnudelete.click += delegate(object s, eventargs ea) { dialogresult dialogresult = devexpress.xtraeditors.xtramessagebox.show(string.format("确定要删除此实验数据吗[{0}]?\r\n删 除后无法恢复!", nodedata.id), "标题", system.windows.forms.messageboxbuttons.yesno, messageboxicon.question); if (dialogresult == dialogresult.yes) { try { string filename = string.empty; #region 删除数据文件 filename = string.format("{0}{1}", nodedata.id, global.main_ext); if (system.io.file.exists(filename)) { system.io.file.delete(filename); } #endregion #region 删除对应的树节点 devexpress.xtratreelist.nodes.treelistnode selectednode = treelist.findnodebykeyid(nodedata.id); if (selectednode != null) { selectednode.parentnode.nodes.remove(selectednode); } #endregion treelist.optionsselection.enableappearancefocusedrow = false; //禁用整行选中 } catch(exception ex) { icsharpcode.core.loggingservice<datatreelisthelper>.error("删除实验数据异常:" + ex.message, ex); devexpress.xtraeditors.xtramessagebox.show("删除实验数据异常:" + ex.message, "标题", messageboxbuttons.ok, messageboxicon.error); } } }; ctxmenu.menuitems.add(mnudelete); #endregion #region 右键弹出上下文菜单 - 重命名数据文件 system.windows.forms.menuitem mnurename = new system.windows.forms.menuitem(); mnurename.text = "重命名"; mnurename.click += delegate(object s, eventargs ea) { //获取当前文件名 string oldname = path.getfilenamewithoutextension(stridvalue); dialog.frmrename frmrename = new frmrename(oldname); frmrename.startposition = formstartposition.centerscreen; dialogresult result = frmrename.showdialog(); if (result == dialogresult.ok) { //刷入框新设置的文件名 string newname = frmrename.filename; //获取原来路径 string filepath = path.getdirectoryname(stridvalue); //使用原来路径加 + 新文件名 结合成新文件路径 string newfilepath = path.combine(filepath, newname); dialogresult dialogresult = devexpress.xtraeditors.xtramessagebox.show(string.format("确定要将实验数据[{0}]重命名为 [{1}]吗?", nodedata.id, newname), "标题", system.windows.forms.messageboxbuttons.yesno, messageboxicon.question); if (dialogresult == dialogresult.yes) { try { string filename = string.empty; string newfilename = string.empty; #region 重命名主通道数据文件 filename = string.format("{0}{1}", nodedata.id, global.main_ext); newfilename = string.format("{0}{1}", newfilepath, global.main_ext); if (system.io.file.exists(filename)) { fileinfo fi = new fileinfo(filename); fi.moveto(newfilename); } #endregion //刷新树 common.datatreelisthelper.triggerrefreshdataevent(); xtramessagebox.show("重命名成功"); treelist.optionsselection.enableappearancefocusedrow = false; //禁用整行选中 } catch (exception ex) { icsharpcode.core.loggingservice<datatreelisthelper>.error("删除实验数据异常:" + ex.message, ex); devexpress.xtraeditors.xtramessagebox.show("删除实验数据异常:" + ex.message, "标题", messageboxbuttons.ok, messageboxicon.error); } } } }; ctxmenu.menuitems.add(mnurename); #endregion #endregion ctxmenu.show(treelist, new system.drawing.point(e.x, e.y)); } return; } } } treelist.optionsselection.enableappearancefocusedrow = false; //禁用整行选中 } }
其中在进行重命名时需要弹出一个窗体
具体实现参照:
winform巧用窗体设计完成弹窗数值绑定-以重命名弹窗为例:
https://blog.csdn.net/badao_liumang_qizhi/article/details/103155532