Example T1101292
Visible to All Users

XAF Blazor - How to create a custom Action type

This example demonstrates how to create a custom Action type with a toggle state.

chrome_RuP8b15xLx

Implementation Details

The Action's implementation consists of the following parts:

  1. A SimpleAction descendant that stores the checked state of the Action (CheckableSimpleAction.cs).
  2. The custom Action binding that binds an abstract Action to its UI representation (CheckableSimpleActionBinding.cs).
  3. The Controller that performs the binding operation. (CheckableSimpleActionController.cs)
  4. The Controller that creates CheckableSimpleAction objects in the UI. (SampleController.cs)

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

CustomActionSolution/CustomActionSolution.Blazor.Server/CustomAction/CheckableSimpleAction.cs
C#
using DevExpress.ExpressApp; using DevExpress.ExpressApp.Actions; using DevExpress.Persistent.Base; namespace CustomActionType.Blazor.Server; public class CheckableSimpleAction : SimpleAction { private bool isChecked = false; public event EventHandler CheckedStateChanged; public CheckableSimpleAction(Controller owner, string id, PredefinedCategory category) : base(owner, id, category) { } public bool Checked { get => isChecked; set { if (isChecked != value) { isChecked = value; OnCheckedStateChanged(); if (Enabled.ResultValue && Active.ResultValue) { DoExecute(); } } } } private void OnCheckedStateChanged() { if (CheckedStateChanged != null) { CheckedStateChanged(this, EventArgs.Empty); } } }
CustomActionSolution/CustomActionSolution.Blazor.Server/CustomAction/CheckableSimpleActionBinding.cs
C#
using DevExpress.ExpressApp.Blazor.Templates.Toolbar.ActionControls; using DevExpress.ExpressApp.Templates.ActionControls; using DevExpress.ExpressApp.Templates.ActionControls.Binding; namespace CustomActionType.Blazor.Server; public class CheckableSimpleActionBinding : ActionBinding { private void Action_CheckedStateChanged(object sender, EventArgs e) { ActionControl.ToolbarItemModel.Checked = Action.Checked; } private void ActionControl_Execute(object sender, EventArgs e) { Action.Checked = !Action.Checked; } public CheckableSimpleActionBinding(CheckableSimpleAction action, DxToolbarItemSimpleActionControl actionControl) : base(action, actionControl) { ((ISimpleActionControl)ActionControl).Execute += ActionControl_Execute; Action.CheckedStateChanged += Action_CheckedStateChanged; ActionControl.ToolbarItemModel.Checked = Action.Checked; } public override void Dispose() { ((ISimpleActionControl)ActionControl).Execute -= ActionControl_Execute; Action.CheckedStateChanged -= Action_CheckedStateChanged; base.Dispose(); } public new CheckableSimpleAction Action { get { return (CheckableSimpleAction)base.Action; } } public new DxToolbarItemSimpleActionControl ActionControl { get { return (DxToolbarItemSimpleActionControl)base.ActionControl; } } }
CustomActionSolution/CustomActionSolution.Blazor.Server/Controllers/CheckableSimpleActionController.cs
C#
using DevExpress.ExpressApp; using DevExpress.ExpressApp.Blazor.Templates.Toolbar.ActionControls; using DevExpress.ExpressApp.SystemModule; using DevExpress.ExpressApp.Templates.ActionControls.Binding; namespace CustomActionType.Blazor.Server.Controllers; public class CheckableSimpleActionController : Controller { protected override void OnActivated() { base.OnActivated(); ActionControlsSiteController actionControlsSiteController = Frame.GetController<ActionControlsSiteController>(); if (actionControlsSiteController != null) { actionControlsSiteController.CustomBindActionControlToAction += ActionControlsSiteController_CustomBindActionControlToAction; } } protected override void OnDeactivated() { ActionControlsSiteController actionControlsSiteController = Frame.GetController<ActionControlsSiteController>(); if (actionControlsSiteController != null) { actionControlsSiteController.CustomBindActionControlToAction -= ActionControlsSiteController_CustomBindActionControlToAction; } base.OnDeactivated(); } private void ActionControlsSiteController_CustomBindActionControlToAction(object sender, CustomBindEventArgs e) { if (e.Action is CheckableSimpleAction checkableSimpleAction && e.ActionControl is DxToolbarItemSimpleActionControl toolbarItemSimpleActionControl) { e.Binding = new CheckableSimpleActionBinding(checkableSimpleAction, toolbarItemSimpleActionControl); } } }
CustomActionSolution/CustomActionSolution.Blazor.Server/Controllers/SampleController.cs
C#
using DevExpress.Data.Filtering; using DevExpress.ExpressApp; using DevExpress.Persistent.Base; using dxTestSolution.Module.BusinessObjects; namespace CustomActionType.Blazor.Server.Controllers; public class SampleController : ObjectViewController<ListView, Order> { private readonly CheckableSimpleAction activeFilterAction; private readonly CheckableSimpleAction todayFilterAction; public SampleController() { activeFilterAction = new CheckableSimpleAction(this, "ActiveFilterAction", PredefinedCategory.Filters); activeFilterAction.Caption = "Active"; activeFilterAction.Execute += ActiveFilterAction_Execute; ; todayFilterAction = new CheckableSimpleAction(this, "TodayFilterAction", PredefinedCategory.Filters); todayFilterAction.Caption = "Today"; todayFilterAction.Execute += TodayFilterAction_Execute; ; } protected override void OnActivated() { EnsureActiveFilterApplied(); EnsureTodayFilterApplied(); } private void ActiveFilterAction_Execute(object sender, DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs e) { EnsureActiveFilterApplied(); } private void TodayFilterAction_Execute(object sender, DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs e) { EnsureTodayFilterApplied(); } private void EnsureActiveFilterApplied() { bool applyFilter = activeFilterAction.Checked; if (applyFilter) { View.CollectionSource.Criteria["IsActive"] = new OperandProperty(nameof(Order.Active)); } else { View.CollectionSource.Criteria.Remove("IsActive"); } } private void EnsureTodayFilterApplied() { bool applyFilter = todayFilterAction.Checked; if (applyFilter) { View.CollectionSource.Criteria["IsToday"] = new FunctionOperator(FunctionOperatorType.IsOutlookIntervalToday, new OperandProperty("Date")); } else { View.CollectionSource.Criteria.Remove("IsToday"); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.