Skip to main content
A newer version of this page is available. .
.NET Framework 4.5.2+

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

  • 4 minutes to read

This topic describes how to hide or disable Action UI elements in code.

Tip

You can use other ways to hide Actions that can be more appropriate for your scenario. See the following topic for details: Ways to Hide or Disable an Action.

The image below shows examples of toolbar buttons that can be removed or disabled.

Hide (disable) an Action Button on the Toolbar

The buttons in the image above are XAF Actions. Each Action belongs to a Controller. Determine the Action’s Controller name to manipulate the Action. The following topics describe how to find an Action’s Controller: Determine an Action’s Controller and Identifier.

After you determine the required Action’s Controller, follow the steps below to remove or disable the Action:

  • Override the Controller‘s OnActivated method.

  • Use the GetController method or the Frame.Controllers (Window.Controllers) property to access the 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 the ActionAttribute defines an Action, the 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 button, call the SetItemValue(String, Boolean) method of the Action’s Active property and pass False as the second parameter.

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

The Managing availability of Controllers and Actions section of the following topic is also helpful: Best practices of creating reusable XAF modules by example of a View Variants module extension.

See the following topic to determine why the current Action is deactivated: Determine Why an Action, Controller or Editor is Inactive.

Examples:

namespace MySolution.Module.Win{  
   public class ViewController1 : ViewController {  
       public ViewController1() {  
           RegisterActions(components);  
       }
       protected override void OnActivated() {  
           base.OnActivated();  

           if (View is ListView && !View.IsRoot && View.ObjectTypeInfo.Type == typeof(DemoTask)) {  
               DevExpress.ExpressApp.SystemModule.LinkUnlinkController linkUnlinkController = Frame.GetController<DevExpress.ExpressApp.SystemModule.LinkUnlinkController>();  
               if(linkUnlinkController != null) {  
                   linkUnlinkController.LinkAction.Active.SetItemValue("myReason", false);
                // The line below disables the Action button
                // linkUnlinkController.LinkAction.Enabled.SetItemValue("myReason", false);
               }
           }
       }
   }
}
namespace MySolution.Module.Win {  
   public class WindowController1 : WindowController {  
       public WindowController1() {  
           RegisterActions(components);  
       }  
       protected override void OnActivated() {  
           base.OnActivated();  
           DevExpress.ExpressApp.Win.SystemModule.ChooseSkinController chooseSkinController = Window.GetController<DevExpress.ExpressApp.Win.SystemModule.ChooseSkinController>();  
           if(chooseSkinController != null) {  
               chooseSkinController.ChooseSkinAction.Active.SetItemValue("myReason", false);
            // The line below disables the Action button
            // chooseSkinController.ChooseSkinAction.Active.SetItemValue("myReason", false);    
           }  
       }  
   }  
}  
namespace MainDemo.Module.Win {  
   public class ViewController1 : ViewController {  
       public ViewController1() {  
           RegisterActions(components);  
       }  
       protected override void OnActivated() {  
           base.OnActivated();  
           if (View.Id == "Contact_LookupListView") {  
               DevExpress.ExpressApp.Win.SystemModule.WinNewObjectViewController winNewObjectViewController = Frame.GetController<DevExpress.ExpressApp.Win.SystemModule.WinNewObjectViewController>();  
               if(winNewObjectViewController != null) {  
                   winNewObjectViewController.NewObjectAction.Active.SetItemValue("myReason", false);
                // The line below disables the Action button
                // winNewObjectViewController.NewObjectAction.Enabled.SetItemValue("myReason", false);
               }  
           }  
       }  
   }  
}  
See Also