Skip to main content
All docs
V25.2
  • DxTreeList.ContextMenus Property

    Specifies available context menus.

    Namespace: DevExpress.Blazor

    Assembly: DevExpress.Blazor.v25.2.dll

    Declaration

    [DefaultValue(TreeListContextMenus.None)]
    [Parameter]
    public TreeListContextMenus ContextMenus { get; set; }

    Property Value

    Type Default Description
    TreeListContextMenus None

    A collection of TreeListContextMenus values.

    Available values:

    Name Description
    None

    No context menus.

    Header

    The header context menu.

    Footer

    The footer context menu.

    DataRow

    The data row context menu.

    All

    All context menus.

    Remarks

    The DevExpress Blazor TreeList allows you to display context menus with predefined and custom commands.

    DevExpress Blazor TreeList - Context Menus

    Run Demo: Context Menu

    Add Context Menus

    You can display context menus for the following TreeList elements:

    Blazor TreeList Context Menu - Supported Regions: Data Row, Footer, Header

    Assign TreeListContextMenus values to the ContextMenus property to display contextual commands when users right-click TreeList elements.

    Note

    • The TreeList does not display a context menu if the menu contains no items.
    • Once you activate a context menu for specific TreeList elements, the browser context menu becomes unavailable for the corresponding region (even if the TreeList context menu is empty).

    The following example demonstrates how to activate/deactivate TreeList context menus:

    @inject EmployeeTaskService EmployeeTaskService
    
    <DxTreeList Data="TreeListData" 
                KeyFieldName="Id" 
                ParentKeyFieldName="ParentId"
                ContextMenu="AvailableContextMenus">
        <Columns>
            <DxTreeListDataColumn FieldName="Name" Caption="Task" />
            <DxTreeListDataColumn FieldName="EmployeeName" />
            <DxTreeListDataColumn FieldName="StartDate" />
            <DxTreeListDataColumn FieldName="DueDate" />
        </Columns>
    </DxTreeList>
    
    @code {
        List<EmployeeTask> TreeListData { get; set; }
        TreeListContextMenus AvailableContextMenus;
    
        protected override void OnInitialized() {
            TreeListData = EmployeeTaskService.GenerateData();
            // Activates all context menus (including menus without built-in items)
            AvailableContextMenus = TreeListContextMenus.All;
            // Disables all context menus
            AvailableContextMenus = TreeListContextMenus.None;
            // Activates only context menus that contain built-in items
            AvailableContextMenus = TreeListContextMenus.Header;
            // Activates menus for all elements except data rows
            AvailableContextMenus = TreeListContextMenus.All & ~TreeListContextMenus.DataRow;
        }
    }
    

    Built-in Items

    The table below lists context menu types and built-in commands available in the TreeList:

    yes - The context menu includes this item by default.
    partially - You can add this item to the context menu.

    Menu Item Data Row Footer Header
    AutoFitAll partially partially yes
    ClearColumnSorting partially partially yes
    CollapseAll partially partially partially
    ExpandAll partially partially partially
    HideColumn partially partially yes
    ShowColumnChooser partially partially yes
    ShowFilterBuilder partially partially yes
    SortColumnAscending partially partially yes
    SortColumnDescending partially partially yes

    The resulting item collection depends on the menu type, TreeList settings, and component state. For instance, a header context menu contains the SortColumnAscending command only if sorting is allowed (on both TreeList and column levels). The ClearColumnSorting command is disabled if the target column is not sorted.

    Customization

    The CustomizeContextMenu event occurs before a context menu appears. Handle the event to customize the built-in item collection or add custom commands.

    The following code snippet customizes commands available in header and data row context menus:

    @inject EmployeeTaskService EmployeeTaskService
    
    <DxTreeList Data="TreeListData" 
                KeyFieldName="Id" 
                ParentKeyFieldName="ParentId"
                ContextMenus="@(TreeListContextMenus.Header | TreeListContextMenus.DataRow)"
                CustomizeContextMenu="CustomizeContextMenu">
        <Columns>
            <DxTreeListSelectionColumn Width="80px"/>
            <DxTreeListDataColumn FieldName="Name" Caption="Task" />
            <DxTreeListDataColumn FieldName="EmployeeName" />
            <DxTreeListDataColumn FieldName="StartDate" />
            <DxTreeListDataColumn FieldName="DueDate" />
        </Columns>
    </DxTreeList>
    
    @code {
        List<EmployeeTask> TreeListData { get; set; }
    
        protected override void OnInitialized() {
            TreeListData = EmployeeTaskService.GenerateData();
        }
        void CustomizeContextMenu(TreeListCustomizeContextMenuEventArgs args) {
            // Customizes context menu commands for the selection column header
            if (args.Context is TreeListHeaderCommandContext headerContext) {    
                if (headerContext.Column is ITreeListSelectionColumn selectionColumn) {
                    var isFixed = selectionColumn.FixedPosition != TreeListColumnFixedPosition.None;
                    string itemText = isFixed ? "Unfix Column" : "Fix Column to the Left";
                    var newValue = isFixed ? TreeListColumnFixedPosition.None : TreeListColumnFixedPosition.Left;
                    args.Items.AddCustomItem(itemText, () => {
                        headerContext.TreeList.BeginUpdate();
                        headerContext.Column.FixedPosition = newValue;
                        headerContext.TreeList.EndUpdate();
                    });
                }
            }
            // Adds context menu commands for data rows
            if (args.Context is TreeListDataRowCommandContext) {
                args.Items.Add(TreeListContextMenuDefaultItemNames.ExpandAll);
                args.Items.Add(TreeListContextMenuDefaultItemNames.CollapseAll);
            }
        }
    }
    

    Blazor TreeList - Customize Context Menu

    Implements

    See Also