Ticket T285859
Visible to All Users
Duplicate

How to display a dropdown without a triangle glyph for a SingleChoiceAction

created 9 years ago

Hi,

In XAF's new object button, when user click the the paper icon it will create a new object in type of the first item in the new action item list . Also there is a downward triangle next to the new object button if there is more than one item in the new action item list; when clicking this triangle, it will show the new action item list in a dropdown.

Our user would like to twist the behaviour into following:

  1. No matter how many items are in the new action item list, the triangle don't show up,
  2. When user click on the new object button, it create an new object directly if there is only one item in the item list; if item list has more than one item, the new object button should show the new action item list in dropdown rather than creating an new object in type of first item in item list.

I know I should override the new action, can you advise me on how I can achieve the described behaviour?

Answers approved by DevExpress Support

created 9 years ago (modified 9 years ago)

Hello Ivan,

By default, XAF allows defining whether an action click should show a dropdown or execute an action immediately using the SingleChoiceAction.ShowItemsOnClick property. You can easily change this property based on the number of the New action items. However, a triangle glyph is always shown by default, and the only way to hide it is to customize the underlying bar item. Please see how to do this in the How to: Customize Action Controls (WinForms) topic. Here is a complete example:

C#
public class CustomizeNewActionController : Controller { private List<BarButtonItem> newActionButtons; protected override void OnActivated() { base.OnActivated(); newActionButtons = new List<BarButtonItem>(); Frame.GetController<ActionControlsSiteController>().CustomizeActionControl += ActionControlsSiteController_CustomizeActionControl; BarActionItemsFactory.CustomizeActionControl += BarActionItemsFactory_CustomizeActionControl; Frame.ViewChanged += Window_ViewChanged; } void Window_ViewChanged(object sender, ViewChangedEventArgs e) { SingleChoiceAction newAction = Frame.GetController<NewObjectViewController>().NewObjectAction; newAction.ItemsChanged += newAction_ItemsChanged; CustomizeNewButtons(newAction); } void newAction_ItemsChanged(object sender, ItemsChangedEventArgs e) { CustomizeNewButtons((SingleChoiceAction)sender); } private void CustomizeNewButtons(SingleChoiceAction newAction) { foreach (BarButtonItem item in newActionButtons) { CustomizeNewButton(item, newAction); } } private void CustomizeNewButton(BarButtonItem item, SingleChoiceAction newAction) { bool actAsDropdown = newAction.Items.Count > 1; if (actAsDropdown) { item.ButtonStyle = BarButtonStyle.DropDown; item.ActAsDropDown = true; item.AllowDrawArrow = false; } else { item.ButtonStyle = BarButtonStyle.Default; item.ActAsDropDown = false; } } protected override void OnDeactivated() { newActionButtons = null; Frame.GetController<ActionControlsSiteController>().CustomizeActionControl -= ActionControlsSiteController_CustomizeActionControl; BarActionItemsFactory.CustomizeActionControl -= BarActionItemsFactory_CustomizeActionControl; Frame.ViewChanged -= Window_ViewChanged; base.OnDeactivated(); } private void ActionControlsSiteController_CustomizeActionControl(object sender, ActionControlEventArgs e) { if (e.ActionControl.ActionId == "New") { BarButtonItem barButtonItem = (BarButtonItem)e.ActionControl.NativeControl; newActionButtons.Add(barButtonItem); CustomizeNewButton(barButtonItem, Frame.GetController<NewObjectViewController>().NewObjectAction); } } private void BarActionItemsFactory_CustomizeActionControl(object sender, CustomizeActionControlEventArgs e) { if (e.Action.Id == "New" && e.Action.Controller.Frame == Frame) { BarButtonItem barButtonItem = (BarButtonItem)e.ActionControl.Control; newActionButtons.Add(barButtonItem); CustomizeNewButton(barButtonItem, (SingleChoiceAction)e.Action); } } }
    Comments (2)

      Thanks for your quick response. Can you provide a solution which is compatible with 14.2.4 as I can't the ActionControlsSiteController in the downloaded documentation? Otherwise we might need to upgrade framework for our project.

      Anatol (DevExpress) 9 years ago

        The following code should work fine in this version:

        C#
        public class CustomizeNewActionController : Controller { private List<BarButtonItem> newActionButtons; protected override void OnActivated() { base.OnActivated(); newActionButtons = new List<BarButtonItem>(); BarActionItemsFactory.CustomizeActionControl += BarActionItemsFactory_CustomizeActionControl; Frame.ViewChanged += Window_ViewChanged; } void Window_ViewChanged(object sender, ViewChangedEventArgs e) { SingleChoiceAction newAction = Frame.GetController<NewObjectViewController>().NewObjectAction; newAction.ItemsChanged += newAction_ItemsChanged; CustomizeNewButtons(newAction); } void newAction_ItemsChanged(object sender, ItemsChangedEventArgs e) { CustomizeNewButtons((SingleChoiceAction)sender); } private void CustomizeNewButtons(SingleChoiceAction newAction) { newAction.ShowItemsOnClick = newAction.Items.Count > 1; foreach (BarButtonItem item in newActionButtons) { CustomizeNewButton(item, newAction); } } private void CustomizeNewButton(BarButtonItem item, SingleChoiceAction newAction) { if (newAction.ShowItemsOnClick) { item.ButtonStyle = BarButtonStyle.DropDown; item.AllowDrawArrow = false; } else { item.ButtonStyle = BarButtonStyle.Default; } } protected override void OnDeactivated() { newActionButtons = null; BarActionItemsFactory.CustomizeActionControl -= BarActionItemsFactory_CustomizeActionControl; Frame.ViewChanged -= Window_ViewChanged; base.OnDeactivated(); } private void BarActionItemsFactory_CustomizeActionControl(object sender, CustomizeActionControlEventArgs e) { if (e.Action.Id == "New" && e.Action.Controller.Frame == Frame) { BarButtonItem barButtonItem = (BarButtonItem)e.ActionControl.Control; newActionButtons.Add(barButtonItem); CustomizeNewButton(barButtonItem, (SingleChoiceAction)e.Action); } } }

        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.