下面的示例演示了如何在代码中创建 RibbonControl 内的上下文标签页面。

在本示例中,创建了一个 RibbonControl 控件,它包含了一个固定被显示的页面 (“Home”) 和两个上下文页面 (“Format” 和 “Clipboard”)。 这两个上下文页面被组合在一个自定义页面类别 (“Selection”) 中。

在创建上下文页面时,它们被隐藏。 稍后通过该类别的 RibbonPageCategory.Visible 属性来使上下文页面可视。

下面的插图展示了代码执行结果:

C#CopyCode image复制代码
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars;

// Create a RibbonControl.
RibbonControl ribbon = new RibbonControl();
// Assign a collection of images that will be used by bar items.
ribbon.Images = imageCollection1;            
// Create a page and associate it with the default category.
RibbonPage pageHome = new RibbonPage("Home");
ribbon.Pages.Add(pageHome);
// Create a custom Selection page category.
RibbonPageCategory catSelection = new RibbonPageCategory("Selection", Color.LightPink, false);
ribbon.PageCategories.Add(catSelection);
// Create two contextual pages (Format an Clipboard)in the Selection category.
RibbonPage contextPageFormat = new RibbonPage("Format");
RibbonPage contextPageClipboard = new RibbonPage("Clipboard");
catSelection.Pages.AddRange(new RibbonPage[] {contextPageFormat, contextPageClipboard});

// Customize the Format page by adding a Format group with two bar items to it.
RibbonPageGroup groupFormat = new RibbonPageGroup("Format");
groupFormat.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);
groupFormat.ItemLinks.AddRange(new BarItem[] {itemCopy, itemCut});
contextPageFormat.Groups.Add(groupFormat);
// Subscribe to an event which fires when any item is clicked.
ribbon.ItemClick += new ItemClickEventHandler(ribbon_ItemClick);

// Add the RibbonControl to the form.
this.Controls.Add(ribbon);

//...

// Make the Selection category visible
catSelection.Visible = true;
// Activate the category's first page.
ribbon.SelectedPage = catSelection.Pages[0];


// Respond to item clicking.
void ribbon_ItemClick(object sender, ItemClickEventArgs e) {
    //...
}

Visual BasicCopyCode image复制代码
Imports DevExpress.XtraBars.Ribbon
Imports DevExpress.XtraBars

' Create a RibbonControl.
Dim ribbon As RibbonControl = New RibbonControl()
' Assign a collection of images that will be used by bar items.
ribbon.Images = imageCollection1
' Create a page and associate it with the default category.
Dim pageHome As RibbonPage = New RibbonPage("Home")
ribbon.Pages.Add(pageHome)
' Create a custom Selection page category.
Dim catSelection As RibbonPageCategory = New RibbonPageCategory("Selection", Color.LightPink, False)
ribbon.PageCategories.Add(catSelection)
' Create two contextual pages (Format an Clipboard)in the Selection category.
Dim contextPageFormat As RibbonPage = New RibbonPage("Format")
Dim contextPageClipboard As RibbonPage = New RibbonPage("Clipboard")
catSelection.Pages.AddRange(New RibbonPage() {contextPageFormat, contextPageClipboard})

' Customize the Format page by adding a Format group with two bar items to it.
Dim groupFormat As RibbonPageGroup = New RibbonPageGroup("Format")
groupFormat.AllowTextClipping = False
' Add two items to the Format group
Dim itemCopy As BarButtonItem = New BarButtonItem(ribbon.Manager, "Copy", 0)
Dim itemCut As BarButtonItem = New BarButtonItem(ribbon.Manager, "Cut", 1)
groupFormat.ItemLinks.AddRange(New BarItem() {itemCopy, itemCut})
contextPageFormat.Groups.Add(groupFormat)
' Subscribe to an event which fires when any item is clicked.
AddHandler ribbon.ItemClick, AddressOf ribbon_ItemClick
'ribbon.ItemClick += New ItemClickEventHandler(ribbon_ItemClick)

' Add the RibbonControl to the form.
Me.Controls.Add(ribbon)

'...

' Make the Selection category visible
catSelection.Visible = True
' Activate the category's first page.
ribbon.SelectedPage = catSelection.Pages(0)


' Respond to item clicking.
Sub ribbon_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
    '...
End Sub