This example demonstrates how to represent an action via BarCheckItem. For this purpose, a SimpleAction descendant ( CheckableSimpleAction ) is implemented, because it is required to save the checked state of the action. To represent a custom action in a UI, the BarCheckItemCheckableSimpleActionControl is used. To bind an abstract action to its UI representation, a custom CheckableSimpleActionBinding is created. The binding operation is performed by CustomActionControlController.
Note that in most cases, it is not required to implement a custom action and action item. It is sufficient to customize the existing action control as described in the How to: Customize Controls Associated with an Action topic.
See Also
- How to create a custom action type with a custom control in Web (ASP.NET webForms)
- XAF Blazor - Implement a custom Action type (ASP.NET Core Blazor Server)
Files to look at:
CheckableSimpleAction.cs
CustomActionControlController.cs
CheckableSimpleActionBinding.cs
BarCheckItemCheckableSimpleActionControl.cs
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using System;
using System.ComponentModel;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
namespace CustomActionControl.Module {
public class CheckableSimpleAction : SimpleAction {
private bool _Checked = false;
private void OnCheckedStateChanged() {
if(CheckedStateChanged != null) {
CheckedStateChanged(this, EventArgs.Empty);
}
}
public CheckableSimpleAction(IContainer container) : base(container) { }
public CheckableSimpleAction(Controller owner, string id, PredefinedCategory category) : base(owner, id, category) { }
public bool Checked {
get { return _Checked; }
set {
if(_Checked != value) {
_Checked = value;
OnCheckedStateChanged();
if(Enabled.ResultValue && Active.ResultValue) {
DoExecute();
}
}
}
}
public event EventHandler CheckedStateChanged;
}
}
C#using System;
using DevExpress.ExpressApp.Win.Templates.Bars.ActionControls;
using DevExpress.XtraBars;
namespace CustomActionControl.Module.Win.ActionControls {
public class BarCheckItemCheckableSimpleActionControl : BarItemActionControl<BarCheckItem> {
private void BarItem_ItemClick(object sender, ItemClickEventArgs args) {
RaiseExecute();
}
protected override void OnEndInit() {
base.OnEndInit();
BarItem.ItemClick += BarItem_ItemClick;
}
protected void RaiseExecute() {
if(IsConfirmed() && Execute != null) {
Execute(this, EventArgs.Empty);
}
}
public BarCheckItemCheckableSimpleActionControl() { }
public BarCheckItemCheckableSimpleActionControl(string actionId, BarCheckItem item) : base(actionId, item) { }
public bool Checked {
get { return BarItem.Checked; }
set { BarItem.Checked = value; }
}
public event EventHandler<EventArgs> Execute;
}
}
C#using System;
using DevExpress.ExpressApp.Templates.ActionControls.Binding;
namespace CustomActionControl.Module.Win.ActionControls {
public class CheckableSimpleActionBinding : ActionBinding {
private void Action_CheckedStateChanged(object sender, EventArgs e) {
ActionControl.Checked = Action.Checked;
}
private void ActionControl_Execute(object sender, EventArgs e) {
Action.Checked = !Action.Checked;
}
public CheckableSimpleActionBinding(CheckableSimpleAction action, BarCheckItemCheckableSimpleActionControl actionControl)
: base(action, actionControl) {
ActionControl.Execute += new EventHandler<EventArgs>(ActionControl_Execute);
Action.CheckedStateChanged += new EventHandler(Action_CheckedStateChanged);
ActionControl.Checked = Action.Checked;
}
public override void Dispose() {
ActionControl.Execute -= new EventHandler<EventArgs>(ActionControl_Execute);
Action.CheckedStateChanged -= new EventHandler(Action_CheckedStateChanged);
base.Dispose();
}
public new CheckableSimpleAction Action {
get { return (CheckableSimpleAction)base.Action; }
}
public new BarCheckItemCheckableSimpleActionControl ActionControl {
get { return (BarCheckItemCheckableSimpleActionControl)base.ActionControl; }
}
}
}
C#using System;
using CustomActionControl.Module.Win.ActionControls;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Templates.ActionControls;
using DevExpress.ExpressApp.Templates.ActionControls.Binding;
using DevExpress.ExpressApp.Win.Templates.Bars.ActionControls;
using DevExpress.ExpressApp.Win.Templates.Ribbon.ActionControls;
using DevExpress.XtraBars;
namespace CustomActionControl.Module.Win.Controllers {
public class CustomActionControlController : Controller {
private void actionControlsSiteController_CustomAddActionControlToContainer(object sender, CustomAddActionControlEventArgs e) {
if(e.Action is CheckableSimpleAction) {
if(e.Container is BarLinkActionControlContainer) {
BarLinkActionControlContainer container = (BarLinkActionControlContainer)e.Container;
IActionControl actionControl = CreateActionControl(e.Action.Id, container.BarContainerItem);
container.AddActionControl(actionControl);
e.Handled = true;
}
if(e.Container is RibbonGroupActionControlContainer) {
RibbonGroupActionControlContainer container = (RibbonGroupActionControlContainer)e.Container;
IActionControl actionControl = CreateActionControl(e.Action.Id, container.RibbonGroup.ItemLinks);
container.AddActionControl(actionControl);
e.Handled = true;
}
}
}
private BarCheckItemCheckableSimpleActionControl CreateActionControl(string id, BarLinksHolder barLinksHolder) {
BarCheckItem barCheckItem = new BarCheckItem(barLinksHolder.Manager);
barCheckItem.CausesValidation = true;
barLinksHolder.AddItem(barCheckItem);
return new BarCheckItemCheckableSimpleActionControl(id, barCheckItem);
}
private void actionControlsSiteController_CustomBindActionControlToAction(object sender, CustomBindEventArgs e) {
if(e.Action is CheckableSimpleAction && e.ActionControl is BarCheckItemCheckableSimpleActionControl) {
e.Binding = new CheckableSimpleActionBinding((CheckableSimpleAction)e.Action, (BarCheckItemCheckableSimpleActionControl)e.ActionControl);
}
}
protected override void OnActivated() {
base.OnActivated();
ActionControlsSiteController actionControlsSiteController = Frame.GetController<ActionControlsSiteController>();
if (actionControlsSiteController != null) {
actionControlsSiteController.CustomAddActionControlToContainer += new EventHandler<CustomAddActionControlEventArgs>(actionControlsSiteController_CustomAddActionControlToContainer);
actionControlsSiteController.CustomBindActionControlToAction += new EventHandler<CustomBindEventArgs>(actionControlsSiteController_CustomBindActionControlToAction);
}
}
protected override void OnDeactivated() {
ActionControlsSiteController actionControlsSiteController = Frame.GetController<ActionControlsSiteController>();
if (actionControlsSiteController != null) {
actionControlsSiteController.CustomAddActionControlToContainer -= new EventHandler<CustomAddActionControlEventArgs>(actionControlsSiteController_CustomAddActionControlToContainer);
actionControlsSiteController.CustomBindActionControlToAction -= new EventHandler<CustomBindEventArgs>(actionControlsSiteController_CustomBindActionControlToAction);
}
base.OnDeactivated();
}
}
}