Skip to main content
.NET 8.0+

Hide or Disable an Action (Button, Menu Item) in Code

  • 3 minutes to read

This topic describes how to hide or disable Action UI elements in code. You can also use other ways to hide/disable Actions that can be more suited to your scenario. See the following topic for details: Ways to Hide or Disable an Action.

The image below shows examples of toolbar buttons you can remove or disable:

Hide (disable) an Action Button on the Toolbar

The buttons in the image above are XAF Actions. Each Action belongs to a Controller.

To customize Controllers in XAF applications, you can inherit Controllers or get their instances in other Controllers. For more information, refer to the following topic: Customize Controllers and Actions.

Follow the steps below to remove or disable an Action from another Controller:

  1. Create a new Controller. Choose the Controller’s base class type depending on the target scope: Define the Scope of Controllers and Actions.

  2. Override the Controller‘s OnActivated method.

  3. Determine the target Action’s Controller class name to manage the Action. For more information on how to find an Action’s Controller, refer to the following topic: Determine an Action's Controller and Identifier.

  4. Use the Frame.GetController method or the Frame.Controllers property to access the Action’s Controller.

  5. Use the Actions property of the Controller class (YourController.Actions["YourActionId"]) or built-in Controller properties (for example, NewObjectViewController.NewObjectAction) to access an Action.

    When ActionAttribute defines an Action, ObjectMethodActionsViewController generates the Action at runtime. XAF uses the following pattern to create the Action’s ID: <short name of the container class> + <name of the method marked with ActionAttribute> (for example, Task.MarkCompleted).

  6. To remove an Action, call the SetItemValue(String, Boolean) method of the Action’s Active property and pass False as the second parameter.

    To disable an Action, call the SetItemValue(String, Boolean) method of the Action’s Enabled property and pass False as the second parameter.

The following Controller hides the FullTextSearch Action in the Paycheck List View:

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.SystemModule;

public class HideFullTextSearchController : ViewController {
    public HideFullTextSearchController() {
        TargetViewId = "Paycheck_ListView";
    }

    private FilterController filterController;
    const string deactivateReason = "HiddenInPaycheck";

    protected override void OnActivated() {
        base.OnActivated();
        filterController = Frame.GetController<FilterController>();
        if (filterController != null) {
            filterController.FullTextFilterAction.Active.SetItemValue(deactivateReason, false);
            // The line below disables the Action button
            // filterController.FullTextFilterAction.Enabled.SetItemValue(deactivateReason, false);
        }
    }

    protected override void OnDeactivated() {
        base.OnDeactivated();
        if(filterController != null) {
            filterController.FullTextFilterAction.Active.RemoveItem(deactivateReason);
            filterController = null;
        }
    }
}

To undo these changes when the Paycheck List View is closed, the Controller removes its custom item from the ActionBase.Active list in the OnDeactivated method. This is necessary because XAF uses the same FilterController instance for every View of the current Window (Frame). For additional information, refer to the following topic: Controllers (UI Logic & Interaction).

Additional links that relate to this topic:

See Also