DevExpress的TreeList實現節點上添加自定義右鍵菜單並實現刪除節點功能:node
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102551175編程
在上面已經實現自定義右鍵菜單刪除樹節點,這裏要實現自定義右鍵菜單實現spa
右鍵功能自定義,好比打開文件選擇框。.net
注:code
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公衆號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。orm
首先綁定treelist的鼠標單擊事件對象
treeList.MouseClick -= treeList_MouseClick;
treeList.MouseClick += treeList_MouseClick;
而後在綁定的單擊事件中blog
獲取treelist,而後獲取其數據源並轉換爲對象List,而後是相關的業務判斷。教程
而後若是是鼠標右鍵的話,新增右鍵菜單項。事件
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; //啓用整行選中 #region 右鍵彈出上下文菜單 - 刪除數據文件 if (e.Button == System.Windows.Forms.MouseButtons.Right) { System.Windows.Forms.ContextMenu ctxMenu = new System.Windows.Forms.ContextMenu(); 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 刪除對應的樹節點 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) { DevExpress.XtraEditors.XtraMessageBox.Show("刪除實驗數據異常:" + ex.Message, "霸道標題", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }; ctxMenu.MenuItems.Add(mnuDelete); #endregion #region 右鍵彈出上下文菜單 - 導入配置文件 System.Windows.Forms.MenuItem mnuImport = new System.Windows.Forms.MenuItem(); mnuImport.Text = "導入配置文件"; mnuImport.Click += delegate(object s, EventArgs ea) { OpenFileDialog importOpenFileDialog = new OpenFileDialog(); importOpenFileDialog.ShowDialog(); }; ctxMenu.MenuItems.Add(mnuImport); #endregion #region 右鍵彈出上下文菜單 - 導出配置文件 System.Windows.Forms.MenuItem mnuExport = new System.Windows.Forms.MenuItem(); mnuExport.Text = "導出配置文件"; mnuExport.Click += delegate(object s, EventArgs ea) { DialogResult dialogResult = DevExpress.XtraEditors.XtraMessageBox.Show(String.Format("導出[{0}]成功!", nodeData.Id), "標題 ", System.Windows.Forms.MessageBoxButtons.YesNo, MessageBoxIcon.Question); }; ctxMenu.MenuItems.Add(mnuExport); #endregion ctxMenu.Show(treeList, new System.Drawing.Point(e.X, e.Y)); } return; } } } treeList.OptionsSelection.EnableAppearanceFocusedRow = false; //禁用整行選中 } }
點擊導入配置文件後