場景
DevExpress的TreeList實作節點上添加自定義右鍵選單并實作洗掉節點功能:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102551175
在上面已經實作自定義右鍵選單洗掉樹節點,這里要實作自定義右鍵選單實作
右鍵功能自定義,比如打開檔案選擇框,
注:
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程式猿
獲取編程相關電子書、教程推送與免費下載,
實作
首先系結treelist的滑鼠單擊事件
treeList.MouseClick -= treeList_MouseClick;
treeList.MouseClick += treeList_MouseClick;
然后在系結的單擊事件中
獲取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 = https://www.cnblogs.com/badaoliumangqizhi/p/null; string strIdValue =https://www.cnblogs.com/badaoliumangqizhi/p/ 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; //禁用整行選中 } }
效果

點擊匯入組態檔后

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/108736.html
標籤:C#
上一篇:C#簡單的列舉及結構
