彩票走势图

DXperience WinForms12.2帮助文档三:在XtraGrid中自定义菜单

原创|其它|编辑:郝浩|2013-01-17 16:33:18.000|阅读 1242 次

概述:XtraGrid是DXperience WinForms Subscription下的一个子控件,我们可以通过XtraGrid 的PopupMenuShowing事件的属性自定义菜单。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

相关链接:

XtraGridDXperience WinForms Subscription下的一个子控件,拥有丰富的菜单选项和网格视图。下面为大家介绍如何在XtraGrid控件中自定义菜单,假设这个菜单的布局如下图所示:

DXperience WinForms Subscription,XtraGrid,网格控件,自定义网格菜单

当在DXperience WinForms网格视图上右键单击鼠标,GridView.PopupMenuShowing事件被触发,Menu参数就会指定要被调用的菜单。但注意,当某一行被右击时,XtraGrid不会显示任何默认的菜单,但PopupMenuShowing 事件仍然会被触发,只不过它的Menu参数引用了一个空菜单。所以我们可以利用这个属性添加自定义菜单。

在这个示例中,要创建下面3个菜单项:

  • "Rows"子菜单显示"Delete Row" 菜单项。这个子菜单由DXSubMenuItem类表示。
  • "Delete Row"菜单项由 DXMenuItem类表示,单击这个选项可以删除选中的行。
  • "Merging Enabled"菜单项由DXMenuCheckItem 类表示,单击这个选项触发GridOptionsView.AllowCellMerge选项,从而判断单元格合并功能是否被启用。

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@pclwef.cn

文章转载自:慧都控件

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP