XtraGrid 控件提供了为任何网格元素 (行指示器面板、数据单元格等) 创建自定义上下文菜单并调用它们的功能。 要实现此功能,则必须创建网格的自定义上下文菜单,并接管网格的鼠标事件 (例如 MouseDown 事件) 来显示此菜单。 本主题展示了如何完成此任务。
弹出式菜单的基本概念在 弹出式菜单概述 主题中描述。
创建自定义上下文菜单
标准网格菜单中使用的菜单项是由 DXMenuItem 对象表示的。 当创建自定义上下文菜单时,与使用 ContextMenu 对象相比较,使用这些菜单项更有优势: 当 bar manager 被指派到网格的 EditorContainer.MenuManager 属性时,这些菜单项的外观将由 bar manager 控制。 在这种情况下,可以使用多种外观与感觉设置 (Flat、Styles3D、OfficeXP、Office2003 等) 来绘制菜单项。 请参阅 弹出式菜单概述 主题来获得更多信息。
下面的示例展示了如何为 标头面板按钮 创建一个网格自定义菜单。
第一步是创建网格自定义菜单。 下面的示例代码创建了表示自定义菜单的 GridViewColumnButtonMenu 类。 此菜单有两个菜单项: Columns 和 Runtime Column Customization。 第一个菜单项是子菜单项,它显示了允许显示/隐藏网格列的子菜单。 第二个菜单项调用 “自定义”窗体。
C# | 复制代码 |
---|---|
using DevExpress.Utils; using DevExpress.Utils.Menu; using DevExpress.XtraGrid.Menu; using DevExpress.XtraGrid.Columns; public class GridViewColumnButtonMenu : GridViewMenu { public GridViewColumnButtonMenu(DevExpress.XtraGrid.Views.Grid.GridView view) : base(view) { } // Create menu items. // This method is automatically called by the menu's public Init method. protected override void CreateItems() { Items.Clear(); DXSubMenuItem columnsItem = new DXSubMenuItem("Columns"); Items.Add(columnsItem); Items.Add(CreateMenuItem("Runtime Column Customization", GridMenuImages.Column.Images[3], "Customization", true)); foreach(GridColumn column in View.Columns) { if(column.IsOption(ColumnOptions.ShowInCustomizationForm)) columnsItem.Items.Add(CreateMenuCheckItem(column.Caption, column.VisibleIndex >= 0, null, column, true)); } } protected override void OnMenuItemClick(object sender, EventArgs e) { if(RaiseClickEvent(sender, null)) return; DXMenuItem item = sender as DXMenuItem; if(item.Tag == null) return; if(item.Tag is GridColumn) { GridColumn column = item.Tag as GridColumn; column.VisibleIndex = column.VisibleIndex >= 0 ? -1 : View.VisibleColumns.Count; } else if(item.Tag.ToString() == "Customization") View.ShowCustomization(); } } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.Utils Imports DevExpress.Utils.Menu Imports DevExpress.XtraGrid.Menu Imports DevExpress.XtraGrid.Columns Public Class GridViewColumnButtonMenu Inherits GridViewMenu Public Sub New(ByVal View As DevExpress.XtraGrid.Views.Grid.GridView) MyBase.New(view) End Sub ' Create menu items. ' This method is automatically called by the menu's public Init method. Protected Overrides Sub CreateItems() Items.Clear() Dim columnsItem As New DXSubMenuItem("Columns") Items.Add(columnsItem) Items.Add(CreateMenuItem("Runtime Column Customization", _ GridMenuImages.Column.Images(3), "Customization", True)) Dim column As GridColumn For Each column In View.Columns If (column.IsOption(ColumnOptions.ShowInCustomizationForm)) Then columnsItem.Items.Add(CreateMenuCheckItem(column.Caption, _ column.VisibleIndex >= 0, Nothing, column, True)) End If Next End Sub Protected Overrides Sub OnMenuItemClick(ByVal sender As Object, ByVal e As EventArgs) If RaiseClickEvent(sender, Nothing) Then Return Dim item As DXMenuItem = CType(sender, DXMenuItem) If item.Tag Is Nothing Then Return If TypeOf item.Tag Is GridColumn Then Dim column As GridColumn = CType(item.Tag, GridColumn) column.VisibleIndex = IIf(column.VisibleIndex >= 0, -1, View.VisibleColumns.Count) ElseIf item.Tag.ToString() = "Customization" Then View.ShowCustomization() End If End Sub End Class |
第二步是接管网格控件的 MouseDown 事件,当最终用户使用鼠标右键单击标头面板按钮时,调用自定义菜单。
C# | 复制代码 |
---|---|
private void gridControl1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { // Check if the end-user has right clicked the grid control. if(e.Button == MouseButtons.Right) DoShowMenu(gridView1.CalcHitInfo(new Point(e.X, e.Y))); } void DoShowMenu(DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hi) { // Create the menu. DevExpress.XtraGrid.Menu.GridViewMenu menu = null; // Check whether the header panel button has been clicked. if(hi.HitTest == DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitTest.ColumnButton) { menu = new GridViewColumnButtonMenu(gridView1); menu.Init(hi); // Display the menu. menu.Show(hi.HitPoint); } } |
Visual Basic | 复制代码 |
---|---|
Private Sub gridControl1_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles gridControl1.MouseDown ' checking if the end-user has right clicked the grid control If e.Button = MouseButtons.Right Then DoShowMenu(gridView1.CalcHitInfo(New Point(e.X, e.Y))) End If End Sub Sub DoShowMenu(ByVal hi As DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo) ' Create the menu. Dim menu As DevExpress.XtraGrid.Menu.GridViewMenu = Nothing ' Check whether the header panel button has been clicked. If hi.HitTest = DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitTest.ColumnButton Then menu = New GridViewColumnButtonMenu(gridView1) menu.Init(hi) ' Display the menu. menu.Show(hi.HitPoint) End If End Sub |
下面的插图展示了运行结果。
在与 XtraGrid 套件一起提供的 GridCustomMenu 指南项目中,可以获得完整的示例代码。