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:
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:
Create a new Controller. Choose the Controller’s base class type depending on the target scope: Define the Scope of Controllers and Actions.
Override the Controller‘s
OnActivated
method.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.
Use the
Frame.GetController
method or theFrame.Controllers
property to access the Action’s Controller.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
).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:
The Managing availability of Controllers and Actions section of the following topic: Best practices of creating reusable XAF modules by example of a View Variants module extension