Skip to main content
.NET 8.0+

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

How to: Support a Context Menu for a Custom WinForms List Editor

  • 2 minutes to read

In XAF applications, List Views can have context menus filled with Actions. For this purpose, the List Editor displaying a List View should support the IRequireContextMenu and IRequireDXMenuManager interfaces. This topic describes how to implement these interfaces in the WinCustomListEditor demonstrated in the How to: Implement a Custom WinForms List Editor topic.

The following image illustrates the context menu invoked for the WinCustomListEditor.

PopupMenuForWinThumbnailEditor

Note

You can see the code implemented here in the FeatureCenter demo installed with XAF. This demo is located in the %PUBLIC%\Documents\DevExpress Demos 24.2\Components\XAF\FeatureCenter.NETFramework.XPO folder by default.

To enable the context menu in a custom List Editor, modify its code in the following manner.

[ListEditor(typeof(IPictureItem))]
public class WinCustomListEditor : ListEditor, /* ...*/ IRequireContextMenu, IRequireDXMenuManager {
    #region IRequireContextMenu Members
    private void BarManager_QueryShowPopupMenu(object sender, QueryShowPopupMenuEventArgs e) {
        if (e.Control != control) {
            e.Cancel = true;
            e.BreakShowPopupMenu = false;
        }
    }
    public void SetMenu(PopupMenu popupMenu, BarManager barManager) {
        barManager.SetPopupContextMenu(control, popupMenu);
        barManager.QueryShowPopupMenu += BarManager_QueryShowPopupMenu;
    }
        #endregion

        #region IRequireDXMenuManager Members
        public void SetMenuManager(IDXMenuManager menuManager) { }
        #endregion
}

If you implement a List Editor using a descendant of the EditorContainer control, initialize the EditorContainer.MenuManager property in the SetMenuManager method.

In the QueryShowPopupMenu event handler, you can specify whether or not to cancel showing the context menu for the current region of the control using the e.Cancel parameter. For instance, you can use the following logic for the GridView control.

GridHitTest hitTest = gridView.CalcHitInfo(gridControl.PointToClient(e.Position)).HitTest;
e.Cancel = !(((hitTest == GridHitTest.Row) || 
    (hitTest == GridHitTest.RowCell) || (hitTest == GridHitTest.EmptyRow) || 
    (hitTest == GridHitTest.RowDetail) || (hitTest == GridHitTest.None)));