Bar Item Behavior Options
- 3 minutes to read
Bar Item Click
For different types of bar item links you need to use different events to implement the required functionality.
When a bar item link that represents a button is clicked, it invokes the BarItem.ItemPress and BarItem.ItemClick events of the corresponding bar item. The ItemPress event occurs before the mouse button is released. The ItemClick event occurs after that. If you want to implement central management for click events, you can subscribe to the RibbonControl’s RibbonControl.ItemPress and RibbonControl.ItemClick events. These events fire when any bar item link is clicked.
For the links that represent check buttons, handle the BarCheckItem.CheckedChanged event to implement specific functionality.
Bar Item Right-Click (Context Menu)
Handle the RibbonControl.ShowCustomizationMenu event to customize a bar item’s context menu.
Example
The following example handles the RibbonControl.ShowCustomizationMenu event to customize the RibbonControl’s context menu.
The example hides the built-in “Add to Quick Access Toolbar” command, and adds a custom “About” command to the menu.
using DevExpress.XtraBars;
using DevExpress.XtraBars.Localization;
private void ribbonControl1_ShowCustomizationMenu(object sender, DevExpress.XtraBars.Ribbon.RibbonCustomizationMenuEventArgs e) {
// Check if the context menu is invoked after a link is right-clicked.
if (e.Link == null) return;
// Locate and hide the "Add to Quick Access Toolbar" command in the context menu
BarItemLink linkAddToQat = e.CustomizationMenu.ItemLinks.Where(link => link.Caption == BarLocalizer.Active.GetLocalizedString(BarString.RibbonToolbarAdd)).FirstOrDefault();
linkAddToQat.Visible = false;
// Check if a custom "About" command has already been created.
BarItemLink menuAboutCommand = e.CustomizationMenu.ItemLinks.Where(link => link.Caption == "About").FirstOrDefault();
// Add the "About" command.
if (menuAboutCommand == null) {
menuAboutCommand = e.CustomizationMenu.AddItem(GetAboutCommand());
// Add a separator before the command.
menuAboutCommand.BeginGroup = true;
}
}
BarItem biAbout;
BarItem GetAboutCommand() {
if (biAbout == null) {
biAbout = new BarButtonItem();
biAbout.Caption = "About";
biAbout.ItemClick += new ItemClickEventHandler(biAbout_ItemClick);
ribbonControl1.Items.Add(biAbout);
}
return biAbout;
}
// The method invoked when the "About" command is clicked.
void biAbout_ItemClick(object sender, ItemClickEventArgs e) {
MessageBox.Show("About");
}