How to: Deactivate (Hide) an Action in Code How to: In the code to disable (hide) button

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.

Visibility by ActionBase.Active property management operations. When this property returns false, the operation is not visible. Follow these steps to deactivate a predefined or custom actions. In the current example, will disable the "delete" operation.

  • 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.

  • Create a disabled controller controller. To learn how to do this, please refer to the text of a controller class.

  • On covering the active method, as follows. This method is performed after the activation operation, and cause the controller. Activation 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.

In the above code, the false value or a true value added to the active BoolList deleted object view controller. Thus, operation of the controller from the view (view list of contacts in this example) to hide the error value is added. This method is similar to the view controller .TargetTototo and ViewController.TargetType.TargetViewType properties controllers, but they do not apply here, because the controller does not automatically enable disabled occur when the view changes.

Note 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.
XAF provides other methods may be better suited to disable the operation of your program. See ActionBase. The theme for more information.
Refer to determine the operation of the controller and the identifier of the topic, understand how to determine which controller provided to hide or disable operation.
To learn how to access the attribute in the existing controller code, and see the operation of a controller custom text.

Guess you like

Origin www.cnblogs.com/foreachlife/p/How-to-Deactivate-Hide-an-Action-in-Code.html