Back to Devexpress

VGridControlBase.PopupMenuShowing Event

windowsforms-devexpress-dot-xtraverticalgrid-dot-vgridcontrolbase-4a0a848e.md

latest9.4 KB
Original Source

VGridControlBase.PopupMenuShowing Event

Allows you to customize built-in context menus or invoke custom menus.

Namespace : DevExpress.XtraVerticalGrid

Assembly : DevExpress.XtraVerticalGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Navigation, DevExpress.Win.VerticalGrid

Declaration

csharp
public event PopupMenuShowingEventHandler PopupMenuShowing
vb
Public Event PopupMenuShowing As PopupMenuShowingEventHandler

Event Data

The PopupMenuShowing event's data class is PopupMenuShowingEventArgs. The following properties provide information specific to this event:

PropertyDescription
AllowGets or sets whether to display the context menu. Inherited from BasePopupMenuShowingEventArgs.
HitInfoTypeIdentifies a grid element located under the popup menu.
InRecordHeaderGets whether the menu is displayed within a record header.
InRowGets whether the menu is displayed within a row.
MenuGets or sets the control’s popup menu that will be shown.
PointGets or sets coordinates of the invoked context menu (top-left corner) relative to the parent control. Inherited from BasePopupMenuShowingEventArgs.
RecordIndexGets the index of a record where the popup menu will be displayed.
RecordObjectGets a data object that corresponds to a row where the popup menu will be displayed.
RowGets the row where the popup menu will be displayed.
ScreenPointGets coordinates of the invoked context menu (top-left corner) relative to the screen. Inherited from BasePopupMenuShowingEventArgs.

The event data class exposes the following methods:

MethodDescription
ShowCustomMenu(IDXDropDownControlEx)Invokes a custom context menu instead of the control’s menu. Inherited from BasePopupMenuShowingEventArgs.
ShowCustomMenu(ContextMenuStrip)Invokes a custom context menu instead of the control’s menu. Inherited from BasePopupMenuShowingEventArgs.

Remarks

If the PropertyGridControl.OptionsMenu.EnableContextMenu option is enabled (see VGridOptionsMenu.EnableContextMenu), the property grid displays a context menu with a right-click on a property.

The menu contains the predefined Reset command that sets the property to its default value. The PopupMenuShowing event allows you to populate the menu with custom commands or invoke a custom menu instead.

csharp
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
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

Items added using this event are also shown when the menu is invoked with a click on a brick button against a property (see PGridOptionsView.ShowRowBrick).

Refer to the following help topic for more information: Context Menu.

Example

This example shows how to add a command that toggles the Enabled property to a context menu.

csharp
using DevExpress.Utils.Menu;
using DevExpress.XtraVerticalGrid;
using DevExpress.XtraVerticalGrid.Rows;

propertyGridControl1.PopupMenuShowing += propertyGridControl1_PopupMenuShowing;
private void propertyGridControl1_PopupMenuShowing(object sender, DevExpress.XtraVerticalGrid.Events.PopupMenuShowingEventArgs e) {
    PropertyGridControl pg = sender as PropertyGridControl;
    VGridHitInfo hi = pg.CalcHitInfo(pg.PointToClient(Cursor.Position));
    if (hi.Row == null || hi.Row.Properties.FieldName != "Enabled") return;
    ToggleMenuItem.Tag = hi.Row;
    e.Menu.Items.Add(ToggleMenuItem);

}
DXMenuItem toggleMenuItem;
protected DXMenuItem ToggleMenuItem {
    get {
        if (toggleMenuItem == null) {
            DXMenuItem item = new DXMenuItem("Toggle");
            item.Click += item_Click;
            toggleMenuItem = item;
        }
        return toggleMenuItem;
    }
}
void item_Click(object sender, EventArgs e) {
    BaseRow row = (sender as DXMenuItem).Tag as BaseRow;
    bool cellValue = (bool)row.Grid.GetCellValue(row, 0);
    row.Grid.SetCellValue(row, 0, !cellValue);
}
vb
Imports DevExpress.Utils.Menu
Imports DevExpress.XtraVerticalGrid
Imports DevExpress.XtraVerticalGrid.Rows

Private Sub propertyGridControl1_PopupMenuShowing(ByVal sender As Object, ByVal e As DevExpress.XtraVerticalGrid.Events.PopupMenuShowingEventArgs) _
    Handles propertyGridControl1.PopupMenuShowing
    Dim pg As PropertyGridControl = TryCast(sender, PropertyGridControl)
    Dim hi As VGridHitInfo = pg.CalcHitInfo(pg.PointToClient(Cursor.Position))
    If hi.Row Is Nothing OrElse hi.Row.Properties.FieldName <> "Enabled" Then
        Return
    End If
    ToggleMenuItem.Tag = hi.Row
    e.Menu.Items.Add(ToggleMenuItem)

End Sub
Private toggleMenuItem1 As DXMenuItem
Protected ReadOnly Property ToggleMenuItem() As DXMenuItem
    Get
        If toggleMenuItem1 Is Nothing Then
            Dim item As New DXMenuItem("Toggle")
            AddHandler item.Click, AddressOf item_Click
            toggleMenuItem1 = item
        End If
        Return toggleMenuItem1
    End Get
End Property
Private Sub item_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim row As BaseRow = TryCast((TryCast(sender, DXMenuItem)).Tag, BaseRow)
    Dim cellValue As Boolean = DirectCast(row.Grid.GetCellValue(row, 0), Boolean)
    row.Grid.SetCellValue(row, 0, Not cellValue)
End Sub

See Also

EnableContextMenu

RowBrickMenuShowing

ShowRowBrick

MenuManager

VGridControlBase Class

VGridControlBase Members

DevExpress.XtraVerticalGrid Namespace