原創|其它|編輯:郝浩|2013-01-17 16:33:18.000|閱讀 1316 次
概述:XtraGrid是DXperience WinForms Subscription下的一個子控件,我們可以通過XtraGrid 的PopupMenuShowing事件的屬性自定義菜單。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
XtraGrid是DXperience WinForms Subscription下的一個子控件,擁有豐富的菜單選項和網格視圖。下面為大家介紹如何在XtraGrid控件中自定義菜單,假設這個菜單的布局如下圖所示:
當在DXperience WinForms網格視圖上右鍵單擊鼠標,GridView.PopupMenuShowing事件被觸發,Menu參數就會指定要被調用的菜單。但注意,當某一行被右擊時,XtraGrid不會顯示任何默認的菜單,但PopupMenuShowing 事件仍然會被觸發,只不過它的Menu參數引用了一個空菜單。所以我們可以利用這個屬性添加自定義菜單。
在這個示例中,要創建下面3個菜單項:
DXperience WinForms XtraGrid自定義菜單示例代碼:
using DevExpress.XtraGrid.Views.Grid; using DevExpress.Utils.Menu; // Occurs when right-clicking within a grid's view. private void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) { GridView view = sender as GridView; // Check whether a row is right-clicked. if (e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Row) { int rowHandle = e.HitInfo.RowHandle; // Delete existing menu items, if any. e.Menu.Items.Clear(); // Add a submenu with a single menu item. e.Menu.Items.Add(CreateRowSubMenu(view, rowHandle)); // Add a check menu item. DXMenuItem item = CreateMergingEnabledMenuItem(view, rowHandle); item.BeginGroup = true; e.Menu.Items.Add(item); } } // Create a submenu with a single DeleteRow item. DXMenuItem CreateRowSubMenu(GridView view, int rowHandle) { DXSubMenuItem subMenu = new DXSubMenuItem("Rows"); DXMenuItem menuItemDeleteRow = new DXMenuItem("&Delete Row", new EventHandler(OnDeleteRowClick), imageCollection1.Images[0]); menuItemDeleteRow.Tag = new RowInfo(view, rowHandle); subMenu.Items.Add(menuItemDeleteRow); return subMenu; } // Create a check menu item that triggers the Boolean AllowCellMerge option. DXMenuCheckItem CreateMergingEnabledMenuItem(GridView view, int rowHandle) { DXMenuCheckItem checkItem = new DXMenuCheckItem("&Merging Enabled", view.OptionsView.AllowCellMerge, null, new EventHandler(OnMergingEnabledClick)); checkItem.Tag = new RowInfo(view, rowHandle); return checkItem; } //The handler for the DeleteRow menu item void OnDeleteRowClick(object sender, EventArgs e) { DXMenuItem item = sender as DXMenuItem; RowInfo info = item.Tag as RowInfo; info.View.DeleteRow(info.RowHandle); } //The handler for the MergingEnabled menu item void OnMergingEnabledClick(object sender, EventArgs e) { DXMenuCheckItem item = sender as DXMenuCheckItem; RowInfo info = item.Tag as RowInfo; info.View.OptionsView.AllowCellMerge = item.Checked; } //... //The class that stores menu specific information class RowInfo { public RowInfo(GridView view, int rowHandle) { this.RowHandle = rowHandle; this.View = view; } public GridView View; public int RowHandle; }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件