下面的示例代码接管了 GridView.ShowGridMenu 事件,在 列标头的上下文菜单 被显示之前定制该菜单。 示例代码清除了默认的菜单项,并添加了一个用于确定在列标头面板中的列标头是否可以被移动的新菜单项。

C#CopyCode image复制代码
using DevExpress.XtraGrid.Menu;
using DevExpress.Utils.Menu;
using DevExpress.XtraGrid.Columns; 
using DevExpress.XtraGrid.Views.Grid;
// ...
private void bandedGridView1_ShowGridMenu(object sender, GridMenuEventArgs e) {
    if(e.MenuType == GridMenuType.Column) {
        GridViewColumnMenu menu = e.Menu as GridViewColumnMenu;
        menu.Items.Clear();
        if(menu.Column != null) {
            menu.Items.Add(CreateCheckItem("Can Moved", menu.Column, null));
        }
    }
}

// Creates a menu item.
DXMenuCheckItem CreateCheckItem(string caption, GridColumn column, Image image) {
    DXMenuCheckItem item = new DXMenuCheckItem(caption, 
      column.IsOption(ColumnOptions.CanMoved), image, new EventHandler(OnCanMovedItemClick));
    item.Tag = new MenuColumnInfo(column);
    return item;
}

// Menu item click handler.
void OnCanMovedItemClick(object sender, EventArgs e) {
    DXMenuCheckItem item = sender as DXMenuCheckItem;
    MenuColumnInfo info = item.Tag as MenuColumnInfo;
    if(info == null) return;
    if(!item.Checked)
        info.Column.Options &= ~ColumnOptions.CanMoved;
    else
        info.Column.Options |= ColumnOptions.CanMoved;
}

// The class that stores menu specific information.
class MenuColumnInfo {
    public MenuColumnInfo(GridColumn column) {
        this.Column = column;                
    }
    public GridColumn Column;
}

Visual BasicCopyCode image复制代码
Imports DevExpress.XtraGrid.Menu
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.Utils.Menu
Imports DevExpress.XtraGrid.Views.Grid
' ...
Private Sub bandedGridView1_ShowGridMenu(ByVal sender As Object, _
  ByVal e As GridMenuEventArgs) Handles bandedGridView1.ShowGridMenu
    If e.MenuType = GridMenuType.Column Then
        Dim menu As GridViewColumnMenu = e.Menu
        menu.Items.Clear()
        If Not Menu.Column Is Nothing Then
            Menu.Items.Add(CreateCheckItem("Can Moved", Menu.Column, Nothing))
        End If
    End If
End Sub 

' Creates a menu item.
Function CreateCheckItem(ByVal caption As String, ByVal column As GridColumn, _
  ByVal image As Image) As DXMenuCheckItem
    Dim item As DXMenuCheckItem = New DXMenuCheckItem(caption, _
      column.IsOption(ColumnOptions.CanMoved), image, _
      New EventHandler(AddressOf OnCanMovedItemClick))
    item.Tag = New MenuColumnInfo(column)
    Return item
End Function

' Menu item click handler.
Sub OnCanMovedItemClick(ByVal sender As Object, ByVal e As EventArgs)
    Dim item As DXMenuCheckItem = sender
    Dim info As MenuColumnInfo = CType(item.Tag, MenuColumnInfo)
    If info Is Nothing Then Return
    If Not item.Checked Then
        info.Column.Options = info.Column.Options And Not ColumnOptions.CanMoved
    Else
        info.Column.Options = info.Column.Options Or ColumnOptions.CanMoved
    End If
End Sub

' The class that stores menu specific information.
Class MenuColumnInfo
    Public Sub New(ByVal column As GridColumn)
        Me.Column = column
    End Sub
    Public Column As GridColumn
End Class