The requested page is not available for the requested platform. You are viewing the content for Default platform.

How to: Deactivate (Hide) an Action in Code

  • 2 min to read

An Action's visibility is managed by the ActionBase.Active property. When this property returns false, an action is invisible. Follow the steps below to deactivate a predefined or custom action. In the current example, the Delete action will be disabled.

  • Create the DeactivateDeleteController Controller. To learn how to do this, refer to the Controller Class article.
  • Override its OnActivated method as shown below. This method is executed after the Action's activation and raises the Controller.Activated event.

    public partial class DeactivateDeleteController : ObjectViewController {
        private const string Key = "Deactivation in code";
        DeleteObjectsViewController DeleteController;
    
        public DeactivateDeleteController() {
            InitializeComponent();
        }
    
        protected override void OnActivated() {
            base.OnActivated();
            DeleteController =
                Frame.GetController<DeleteObjectsViewController>();
    
            if(DeleteController != null) {
                DeleteController.Active[Key] =
                    !(View.ObjectTypeInfo.Type == typeof(Contact) && View is ListView);
            }
        }
    
        protected override void OnDeactivated() {
            if(DeleteController != null) {
                DeleteController.Active.RemoveItem(Key);
                DeleteController = null;
            }
            base.OnDeactivated();
        }
    }
    

    In the code above, a false or true value is added to the Active BoolList of the DeleteObjectsViewController. As a result, the Controller's Actions will be hidden from the Views for which the false value is added (Contact List Views in this example). This approach is similar to the Controller's ViewController.TargetObjectType and ViewController.TargetViewType properties, but they are not applicable here because enabling a disabled controller does not occur automatically on a View change.

Note

XAF provides other ways to deactivate an action that might be more appropriate for your scenario. Refer to the ActionBase.Active topic to learn more.

Refer to the Determine an Action's Controller and Identifier topic to learn how to determine which Controller provides the Action to be hidden or deactivated.

To learn about the ways to access the existing controller's properties in code, refer to the Customize Controllers and Actions article.

See Also