Search This Blog

Tuesday, March 7, 2017

How to customize the New Object Action with a single item to display a simple button instead of a dropdown in WinForms


I would like to promote an updated/simplified v16.2 solution for this task after introduction of the ActionBase > CustomizeControl event. To remind you the context, let me quote myself from the https://www.devexpress.com/kb=Q497985 thread:



"XAF allows defining whether a SingleChoiceAction click should show a dropdown or execute an action item immediately using the ShowItemsOnClick and DefaultItemMode  properties. You can easily change these properties based on the number of the New action items. However, a triangle glyph is always shown by default. The SingleChoiceAction does not currently provide built-in means for replacing a dropdown with a simple button either. Currently, to improve user experience with such configurations, you can customize the underlying bar item in the YourSolutionName.Module.Win project.


You can use the following controller to hide a drop-down list from the NewObjectAction in case it contains only one element:


using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Win.SystemModule;
using DevExpress.XtraBars;
...
namespace YourSolutionName.Module.Win.Controllers {
    public class CustomizeNewActionController : WindowController {
        BarButtonItem newActionBarButtonItem;
        protected override void OnFrameAssigned() {
            base.OnFrameAssigned();
            WinNewObjectViewController newObjectViewController = Frame.GetController<WinNewObjectViewController>();
            SingleChoiceAction newObjectAction = newObjectViewController.NewObjectAction;
            newObjectAction.CustomizeControl += NewObjectAction_CustomizeControl;
            newObjectAction.ItemsChanged += NewObjectAction_ItemsChanged;
        }
        private void NewObjectAction_ItemsChanged(object sender, ItemsChangedEventArgs e) {
            if(newActionBarButtonItem != null) {
                SingleChoiceAction newObjectAction = (SingleChoiceAction)sender;
                int choicesCount = newObjectAction.Items.Count;
                if(choicesCount == 1) {
                    newActionBarButtonItem.ButtonStyle = BarButtonStyle.Default;
                }
            }
        }
        private void NewObjectAction_CustomizeControl(object sender, CustomizeControlEventArgs e) {
            newActionBarButtonItem = e.Control as BarButtonItem;
        }
    }
}




Here are screenshots showing the results in the application UI before and after this customization.

Before


After



Also, you can use the same approach with the CloneObjectAction (it is a part of the CloneObjectViewController). Refer to the following documentation articles to learn more about this solution:
    Concepts > Extend Functionality > Built-in Controllers and Actions 
    Concepts > Extend Functionality > Customize Controllers and Actions 
    Task-Based Help > How to: Customize Action Controls
    DevExpress.XtraBars > BarButtonItem > ButtonStyle 
    DevExpress.XtraBars > BarButtonItem > ActAsDropDown 


For older versions
Check out the How to display a dropdown without a triangle glyph for a SingleChoiceAction ticket for code examples."

No comments:

Post a Comment