Example E1977
Visible to All Users

How to create a custom WinForms action type with a custom control (BarCheckItem), associated with it

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

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

EFCore/CustomActionEF/CustomActionEF.Module/CheckableSimpleAction.cs
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; } }
EFCore/CustomActionEF/CustomActionEF.Win/ActionControls/BarCheckItemCheckableSimpleActionControl.cs
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; } }
EFCore/CustomActionEF/CustomActionEF.Win/ActionControls/CheckableSimpleActionBinding.cs
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; } } } }
EFCore/CustomActionEF/CustomActionEF.Win/Controllers/CustomActionControlController.cs
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(); } } }

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.