This example demonstrates how to create a custom Action type with a toggle state.
Implementation Details
The Action's implementation consists of the following parts:
- A
SimpleAction
descendant that stores the checked state of the Action (CheckableSimpleAction.cs). - The custom Action binding that binds an abstract Action to its UI representation (CheckableSimpleActionBinding.cs).
- The Controller that performs the binding operation. (CheckableSimpleActionController.cs)
- The Controller that creates
CheckableSimpleAction
objects in the UI. (SampleController.cs)
Files to Review
- CheckableSimpleAction.cs
- CheckableSimpleActionBinding.cs
- CheckableSimpleActionController.cs
- SampleController.cs
Documentation
More Examples
- How to create a custom action type with a custom control (BarCheckItem), associated with it (WinForms)
- How to create a custom action type with a custom control (ASP.NET Web Forms)
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
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);
}
}
}
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; }
}
}
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);
}
}
}
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");
}
}
}