Skip to main content
All docs
V24.2

Add a Simple Action

  • 3 minutes to read

This lesson explains how to create a Simple Action — a button that executes custom code when a user clicks it.

The instructions below demonstrate how to add the Clear tasks button to the Employee Detail View. A click on this button clears all Tracked Tasks of the selected Employee.

Note

Before you proceed, take a moment to review the previous lessons:

Step-by-Step Instructions

  1. Add a View Controller. In the Solution Explorer, right-click the Controllers folder in the MySolution.Module project and choose Add DevExpress Item | New Item… to invoke the Template Gallery. Select the XAF Controllers | View Controller Visual Studio template, specify ClearContactTasksController as the new item’s name, and click Add Item.

    Select ViewController template

  2. Visual Studio displays an autogenerated ClearContactTasksController.cs file with a single View Controller declaration. In the controller constructor, specify the controller properties:

    // ...
    using MySolution.Module.BusinessObjects;
    
    // ...
    public partial class ClearContactTasksController : ViewController {
        public ClearContactTasksController() {
            InitializeComponent();
            //Activate the Controller only in the Detail View
            TargetViewType = ViewType.DetailView;
            //Specify the type of objects that can use the Controller
            TargetObjectType = typeof(Employee);
        }
    // ...
    }
    

    If you do not specify the TargetObjectType property, the application displays the controller’s actions for all Detail Views.

    Tip

    You can also create a generic ViewController<ViewType> or ObjectViewController<ViewType, ObjectType> object and specify the target type as the ViewType parameter. For more information on how to customize a controller’s functionality, refer to the following topic: Define the Scope of Controllers and Actions.

  3. Add a new action to the controller and a handler for the action’s Execute event:

    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.Actions;
    using DevExpress.Persistent.Base;
    using MySolution.Module.BusinessObjects;
    // ...
    public partial class ClearEmployeeTasksController : ViewController {
        public ClearEmployeeTasksController() {
            InitializeComponent();
            TargetViewType = ViewType.DetailView;
            TargetObjectType = typeof(Employee);
    
            SimpleAction clearTasksAction = new SimpleAction(this, "ClearTaskAction", PredefinedCategory.View) {
                //Specify the Action's button caption.
                Caption = "Clear tasks",
                //Specify the confirmation message that pops up when a user executes an Action.
                ConfirmationMessage = "Are you sure you want to clear the Tasks list?",
                //Specify the icon of the Action's button in the interface.
                ImageName = "Action_Clear"
            };
            //This event fires when a user clicks the Simple Action control. Handle this event to execute custom code.
            clearTasksAction.Execute += ClearTasksAction_Execute;
        }
        private void ClearTasksAction_Execute(Object sender, SimpleActionExecuteEventArgs e) {
            while(((Employee)View.CurrentObject).DemoTasks.Count > 0) {
                ((Employee)View.CurrentObject).DemoTasks.Remove(((Employee)View.CurrentObject).DemoTasks[0]);
                ObjectSpace.SetModified(View.CurrentObject, View.ObjectTypeInfo.FindMember(nameof(Employee.DemoTasks)));
            }
        }
    // ...
    }
    

    You can use one of the standard images or import your own to customize the Action button’s icon.

  4. Run the application.

    Open a Detail View for an Employee item. Link several tasks to this item and save it.

    Click the Clear tasks button. A confirmation message appears. Click OK to remove all Tasks from the current Employee.

    ASP.NET Core Blazor
    ASP.NET Core Blazor simple action remove assigned tasks
    Windows Forms
    Windows Forms simple action remove assigned tasks

You can display an action’s button in a Detail View layout instead of a toolbar. Refer to the following topic for more information: How to: Include an Action in a Detail View Layout.

Note

CodeRush allows you to add Actions and Controllers with a few keystrokes. To learn about the Code Templates for XAF, refer to the following help topic: XAF Templates.

Next Lesson

Add a Parametrized Action

See Also