Hi!
I need to create ribbon page groups and some ribbon categories too. I was using templates as suggested here https://www.devexpress.com/Support/Center/Question/Details/Q454345, but in my last post https://www.devexpress.com/Support/Center/Question/Details/Q556866 Dennis told that this was not a good idea and told me to create a new ticket for it.
So my question is: what is the best way to organize my actions in custom ribbon groups and categ ories?
Thanks
The best way to create ribbon page group and categories in XAF application
Answers approved by DevExpress Support
In XAF v14.2, you can customize Ribbon form templates within the designer (see New Ribbon Templates for WinForms). Refer to the How to: Create a Custom WinForms Ribbon Template topic for additional information.
This answer applies to old XAF versions. Starting with version 14.2, we introduced new templates that provide convenience design-time customization capabilities. We suggest using the solution provided in the first answer if you are using the latest XAF version.
There are two possible approaches for adding custom Ribbon pages and categories:
- Via templates customization as per Custom action container, Adding a dynamic Reports menu to the main window menu;
- Via code;
Each of them requires different coding, configuration and maintenance efforts. In my opinion, customizing the ribbon control in code seems to be easier and requires less work. You can consider the following WindowController that works for the main application window and adds a custom Ribbon page with a group and a couple of items:
C#using System;
using System.Linq;
using DevExpress.XtraBars;
using DevExpress.ExpressApp;
using DevExpress.XtraBars.Ribbon;
using System.Collections.Generic;
using DevExpress.ExpressApp.Win.Templates;
using DevExpress.ExpressApp.Win.Templates.ActionContainers;
namespace MainDemo.Module.Win.Controllers {
public class Class1 : WindowController{
private XtraFormTemplateBase template;
public Class1() {
TargetWindowType = WindowType.Main;
}
protected override void OnActivated() {
base.OnActivated();
Window.TemplateChanged += new EventHandler(Window_TemplateChanged);
}
protected override void OnDeactivated() {
Window.TemplateChanged -= new EventHandler(Window_TemplateChanged);
if(template != null && template.RibbonTransformer != null) {
template.RibbonTransformer.Transformed -= new EventHandler<EventArgs>(RibbonTransformer_Transformed);
template = null;
}
base.OnDeactivated();
}
void Window_TemplateChanged(object sender, EventArgs e) {
template = ((Window)sender).Template as XtraFormTemplateBase;
if(template != null) {
template.RibbonTransformer.Transformed += new EventHandler<EventArgs>(RibbonTransformer_Transformed);
}
}
void RibbonTransformer_Transformed(object sender, EventArgs e) {
RibbonControl ribbon = ((ClassicToRibbonTransformer)sender).Ribbon;
ribbon.BeginInit();
//Dennis: Based on the code from: http://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraBarsRibbonRibbonPagetopic
// Create a new page and add it to the ribbon.
RibbonPage ribbonPage = new RibbonPage("Format");
ribbon.Pages.AddRange(new RibbonPage[] { ribbonPage });
// Customize the Format page by adding a Format group with two bar items to it.
RibbonPageGroup ribbonPageGroup = new RibbonPageGroup("Format");
ribbonPageGroup.AllowTextClipping = false;
// Add two items to the Format group
BarButtonItem itemCopy = new BarButtonItem(ribbon.Manager, "Copy", 0);
BarButtonItem itemCut = new BarButtonItem(ribbon.Manager, "Cut", 1);
ribbonPageGroup.ItemLinks.AddRange(new BarItem[] { itemCopy, itemCut });
ribbonPage.Groups.Add(ribbonPageGroup);
ribbon.EndInit();
}
}
}
Please refer to the XtraBars documentation to learn more about possible ribbon customizations.
@Alex:
>>I would prefer to generate the extra ribbon items by code.
Please create a separate ticket in the Support Center and elaborate more on your business requirements and scenario so we could provide you with the best technical solution for it. You are correct that you need a different mechanism with the V2 templates.
Hello Edilson,
Templates customization usually requires more maintenance effort than customizing the ribbon control in code. I will provide more information on this as soon as I can.