原創(chuàng)|其它|編輯:郝浩|2013-01-17 16:33:18.000|閱讀 1316 次
概述:XtraGrid是DXperience WinForms Subscription下的一個子控件,我們可以通過XtraGrid 的PopupMenuShowing事件的屬性自定義菜單。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
XtraGrid是DXperience WinForms Subscription下的一個子控件,擁有豐富的菜單選項(xiàng)和網(wǎng)格視圖。下面為大家介紹如何在XtraGrid控件中自定義菜單,假設(shè)這個菜單的布局如下圖所示:
當(dāng)在DXperience WinForms網(wǎng)格視圖上右鍵單擊鼠標(biāo),GridView.PopupMenuShowing事件被觸發(fā),Menu參數(shù)就會指定要被調(diào)用的菜單。但注意,當(dāng)某一行被右擊時,XtraGrid不會顯示任何默認(rèn)的菜單,但PopupMenuShowing 事件仍然會被觸發(fā),只不過它的Menu參數(shù)引用了一個空菜單。所以我們可以利用這個屬性添加自定義菜單。
在這個示例中,要創(chuàng)建下面3個菜單項(xiàng):
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; }
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件