Back to Devexpress

Context Menu

windowsforms-401867-controls-and-libraries-property-grid-context-menu.md

latest5.6 KB
Original Source

Context Menu

  • Mar 04, 2024
  • 3 minutes to read

The Property Grid can display a context menu. To invoke the menu, users can right-click a property or click a property marker (in Office View only). The menu contains the predefined Reset command that sets the property to its default value. You can also handle a dedicated event to add custom commands to the menu.

Use the PropertyGridControl.OptionsMenu property to access the EnableContextMenu option that allows you to disable the context menu.

Add Commands to Menu

To populate the menu with custom commands, handle the PopupMenuShowing event.

csharp
using DevExpress.Utils.Menu;

DXMenuItem createDataBindingItem;
protected DXMenuItem CreateDataBindingItem {
    get {
        if (createDataBindingItem == null) {
            DXMenuItem item = new DXMenuItem("Create Data Binding...");
            item.Click += (s, ee) => MessageBox.Show("'Create Data Binding...' is clicked.");
            createDataBindingItem = item;
        }
        return createDataBindingItem;
    }
}
private void Grid_PopupMenuShowing(object sender, Events.PopupMenuShowingEventArgs e) {
    if(e.Row.Properties.FieldName == "Appearance.BackColor")
        e.Menu.Items.Add(CreateDataBindingItem);
}
vb
Imports DevExpress.Utils.Menu

Private createDataBindingItem1 As DXMenuItem
Protected ReadOnly Property CreateDataBindingItem() As DXMenuItem
    Get
        If createDataBindingItem1 Is Nothing Then
            Dim item As New DXMenuItem("Create Data Binding...")
            AddHandler item.Click, Sub(s, ee) MessageBox.Show("'Create Data Binding...' is clicked.")
            createDataBindingItem1 = item
        End If
        Return createDataBindingItem1
    End Get
End Property

Private Sub propertyGridControl1_PopupMenuShowing(ByVal sender As Object, ByVal e As DevExpress.XtraVerticalGrid.Events.PopupMenuShowingEventArgs) _
    Handles propertyGridControl1.PopupMenuShowing
    If e.Row.Properties.FieldName = "Appearance.BackColor" Then
        e.Menu.Items.Add(CreateDataBindingItem)
    End If
End Sub

Note

When a user clicks a property marker, the context menu contains commands added in PopupMenuShowing and RowBrickMenuShowing event handlers.

Invoke Custom Menus

You can use the PopupMenuShowing event to display custom menus (PopupMenu) when a user right-clicks a specific element. Create a custom context menu and pass it to the e.ShowCustomMenu method:

csharp
void OnPropertyGridPopupMenuShowing(object sender, Events.PopupMenuShowingEventArgs e) {
    if (e.Row is PGridOfficeCategoryRow) {
        popupMenu_Category.Tag = e.Row;
        e.ShowCustomMenu(popupMenu_Category);
    }
}

PGridOfficeCategoryRow GetCategoryRow(BarItemLink link) {
    PopupMenu menu = link.LinkedObject as PopupMenu;
    return menu.Tag as PGridOfficeCategoryRow;
}

void barButtonItem_FullCollapse_ItemClick(object sender, XtraBars.ItemClickEventArgs e) {
    PGridOfficeCategoryRow row = GetCategoryRow(e.Link);
    row.Grid.CollapseAllRows();
}

void barButtonItem_FullExpand_ItemClick(object sender, XtraBars.ItemClickEventArgs e) {
    PGridOfficeCategoryRow row = GetCategoryRow(e.Link);
    row.Grid.ExpandAllRows();
}
vb
Private Sub OnPropertyGridPopupMenuShowing(ByVal sender As Object, ByVal e As Events.PopupMenuShowingEventArgs)
    If TypeOf e.Row Is PGridOfficeCategoryRow Then
        popupMenu_Category.Tag = e.Row
        e.ShowCustomMenu(popupMenu_Category)
    End If
End Sub

Private Function GetCategoryRow(ByVal link As BarItemLink) As PGridOfficeCategoryRow
    Dim menu As PopupMenu = TryCast(link.LinkedObject, PopupMenu)
    Return TryCast(menu.Tag, PGridOfficeCategoryRow)
End Function

Private Sub barButtonItem_FullCollapse_ItemClick(ByVal sender As Object, ByVal e As XtraBars.ItemClickEventArgs)
    Dim row As PGridOfficeCategoryRow = GetCategoryRow(e.Link)
    row.Grid.CollapseAllRows()
End Sub

Private Sub barButtonItem_FullExpand_ItemClick(ByVal sender As Object, ByVal e As XtraBars.ItemClickEventArgs)
    Dim row As PGridOfficeCategoryRow = GetCategoryRow(e.Link)
    row.Grid.ExpandAllRows()
End Sub

Use the PopupMenuBase.Tag property to pass information about a clicked element to your menu.

Cheat Sheets and Best Practices

Read the following quick-reference guide for general information and examples:

Context Menus - WinForms Cheat Sheets