Context Menu
- 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.
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);
}
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:
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();
}
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: