本主题提供了关于定制弹出式菜单的信息。 弹出式菜单的基本概念在 弹出式菜单概述 主题中描述。
为弹出式菜单实现自定义行为
基本上,通过使用 GridView.OptionsMenu 属性,可以允许或禁止显示上下文菜单。 要更好地控制上下文菜单,则可以使用 GridView.ShowGridMenu 事件。 如果 GridView.OptionsMenu 属性允许菜单调用,则每当最终用户试图调用上下文菜单时,GridView.ShowGridMenu 事件都发生。
我们详细考虑 GridView.ShowGridMenu 事件。 此事件的数据是由 GridMenuEventArgs 类提供的。 此事件的参数允许标识被显示的菜单,定制此菜单或停止它被调用。
下面的示例接管了 GridView.ShowGridMenu 事件来创建一个自定义的列标头上下文菜单。 GridMenuEventArgs.Menu 参数用于访问当前菜单。 完整的代码可以在 Fixed Columns 演示中中找到。
C# | 复制代码 |
---|---|
using DevExpress.XtraGrid.Columns; using DevExpress.XtraGrid.Views.Grid; using DevExpress.Utils.Menu; using DevExpress.XtraGrid.Menu; // ... private void gridView1_ShowGridMenu(object sender, GridMenuEventArgs e) { if(e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Column) { DevExpress.XtraGrid.Menu.GridViewColumnMenu menu = e.Menu as GridViewColumnMenu; //Erasing the default menu items menu.Items.Clear(); if(menu.Column != null) { //Adding new items menu.Items.Add(CreateCheckItem("Fixed None", menu.Column, FixedStyle.None, imageList2.Images[0])); menu.Items.Add(CreateCheckItem("Fixed Left", menu.Column, FixedStyle.Left, imageList2.Images[1])); menu.Items.Add(CreateCheckItem("Fixed Right", menu.Column, FixedStyle.Right, imageList2.Images[2])); } } } //Create a menu item DXMenuCheckItem CreateCheckItem(string caption, GridColumn column, FixedStyle style, Image image) { DXMenuCheckItem item = new DXMenuCheckItem(caption, column.Fixed == style, image, new EventHandler(OnFixedClick)); item.Tag = new MenuInfo(column, style); return item; } //Menu item click handler void OnFixedClick(object sender, EventArgs e) { DXMenuItem item = sender as DXMenuItem; MenuInfo info = item.Tag as MenuInfo; if(info == null) return; info.Column.Fixed = info.Style; } //The class that stores menu specific information class MenuInfo { public MenuInfo(GridColumn column, FixedStyle style) { this.Column = column; this.Style = style; } public FixedStyle Style; public GridColumn Column; } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.Utils.Menu Imports DevExpress.XtraGrid.Columns Imports DevExpress.XtraGrid.Views.Grid Imports DevExpress.XtraGrid.Menu Private Sub gridView1_ShowGridMenu(ByVal sender As Object, ByVal e As GridMenuEventArgs) _ Handles gridView1.ShowGridMenu If e.MenuType = GridMenuType.Column Then Dim menu As DevExpress.XtraGrid.Menu.GridViewColumnMenu = CType(e.Menu, GridViewColumnMenu) 'Erasing the default menu items menu.Items.Clear() If Not menu.Column Is Nothing Then 'Adding new items menu.Items.Add(CreateCheckItem("Fixed None", menu.Column, FixedStyle.None, imageList2.Images(0))) menu.Items.Add(CreateCheckItem("Fixed Left", menu.Column, FixedStyle.Left, imageList2.Images(1))) menu.Items.Add(CreateCheckItem("Fixed Right", menu.Column, FixedStyle.Right, imageList2.Images(2))) End If End If End Sub 'Create a menu item Function CreateCheckItem(ByVal caption As String, ByVal column As GridColumn, _ ByVal style As FixedStyle, ByVal image As Image) As DXMenuCheckItem Dim item As DXMenuCheckItem = New DXMenuCheckItem(caption, _ column.Fixed = style, image, New EventHandler(AddressOf OnFixedClick)) item.Tag = New MenuInfo(column, style) Return item End Function 'Menu item click handler Sub OnFixedClick(ByVal sender As Object, ByVal e As EventArgs) Dim item As DXMenuItem = CType(sender, DXMenuItem) Dim info As MenuInfo = CType(item.Tag, MenuInfo) If info Is Nothing Then Exit Sub info.Column.Fixed = info.Style End Sub 'The class that stores menu specific information Class MenuInfo Public Sub New(ByVal column As GridColumn, ByVal style As FixedStyle) Me.Column = column Me.Style = style End Sub Public Style As FixedStyle Public Column As GridColumn End Class |
另外,可以通过 GridMenuEventArgs.Allow 参数来停止调用菜单。 GridMenuEventArgs.Point 参数获取或设置上下文菜单的位置。
GridView.GridMenuItemClick 事件发生,以响应菜单项被单击。 当单击特定菜单项时,它允许取消执行默认的操作,并/或提供自定义处理。 下面的示例接管了 GridView.GridMenuItemClick 事件,来修改 Sub Total 列中的汇总格式。 这是必要的,因为当使用上下文菜单修改汇总类型时,对汇总应用默认的格式设置。 因此,下面的事件处理程序修改了所应用的格式字符串,来使用货币格式显示汇总值。
GridMenuItemClickEventArgs.MenuType 和 GridMenuItemClickEventArgs.DXMenuItem 参数用于确定被单击的菜单项。 GridMenuItemClickEventArgs.SummaryFormat 参数表示当前的汇总格式。
C# | 复制代码 |
---|---|
using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Localization; //... private void bandedGridView1_GridMenuItemClick(object sender, GridMenuItemClickEventArgs e) { if(e.MenuType != GridMenuType.Summary || e.DXMenuItem.Tag.Equals(GridStringId.MenuFooterCount)) return; if(e.Column.FieldName != "ID" && e.Column.FieldName != "Customer Name" && e.Column.FieldName != "Year") { string sumFormat = e.SummaryFormat; int subSumFormat = sumFormat.IndexOf("{0"); if(subSumFormat > 0) sumFormat = (e.DXMenuItem.Tag.Equals(GridStringId.MenuFooterSum) ? "" : sumFormat.Substring(0, subSumFormat)) + "{0:c}"; e.SummaryFormat = sumFormat; } } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraGrid.Views.Grid Imports DevExpress.XtraGrid.Localization '... Private Sub bandedGridView1_GridMenuItemClick(ByVal sender As Object, _ ByVal e As GridMenuItemClickEventArgs) _ Handles bandedGridView1.GridMenuItemClick If e.MenuType <> GridMenuType.Summary Or _ e.DXMenuItem.Tag.Equals(GridStringId.MenuFooterCount) Then Exit Sub If e.Column.FieldName <> "ID" And e.Column.FieldName <> "Customer Name" _ And e.Column.FieldName <> "Year" Then Dim sumFormat As String = e.SummaryFormat Dim subSumFormat As Integer = sumFormat.IndexOf("{0") If subSumFormat > 0 Then sumFormat = IIf(e.DXMenuItem.Tag.Equals(GridStringId.MenuFooterSum), _ "", sumFormat.Substring(0, subSumFormat)) + "{0:c}" End If e.SummaryFormat = sumFormat End If End Sub |