这个示例展示了如何创建一个 RibbonControl,并把一个页面 (“Home”) 和两个组 (“File” 和 “File 2”) 添加到该页面中。 下面的插图展示了代码运行结果:
要把 bar items (命令、子菜单、静态文本等) 添加到组中,则必须创建特定的 bar items。 在本例中创建了 BarButtonItem 对象来呈现按钮,单此这些按钮可以执行特定的功能。 要显示组内的或者 RibbonControl 中的某个 bar item,必须创建一个 bar item link。 通常,不需要直接创建 bar item link,因为当 bar item 被添加到组中时,会自动创建相应的 lnik。 使用 links 允许同时在多个位置 (组、菜单等) 呈现单个 bar item。 请参阅在 “File” 和 “File 2” 组中的 “Open...” 命令。 它们是两个不同的 links,但呈现了相同的 bar item。 要获得更多关于 bar Items 和 links 的信息,请参阅 Bar Items 和 Links 主题。
为了确保 RibbonControl 内 bar item 的适当功能,它必须被添加到 RibbonControl.Items 集合中。 否则,该 bar item 的功能可能不是所预期的 (例如,子菜单项可能不会被显示)。 本示例展示了三种创建 bar button items 的正确方法。
- 使用 RibbonControl.Items 集合的 BarItems.CreateButton 方法。 所创建的 item 被自动添加到 RibbonControl.Items 集合中;
- 使用 BarButtonItem 类的有 manager 参数的构造函数。 把 RibbonControl.Manager 属性指定的对象作为 manager 参数传递。 在这种情况下,所创建的 item 将被添加到 RibbonControl.Items 集合中。
- 使用 BarButtonItem 类的默认构造函数。 所创建的 bar item 必须被人工添加到 RibbonControl.Items 集合中。
C# | 复制代码 |
---|---|
using DevExpress.XtraBars.Ribbon; using DevExpress.XtraBars; // Create a RibbonControl RibbonControl ribbonControl = new RibbonControl(); this.Controls.Add(ribbonControl); // Assign the image collection that will provide images for bar items. ribbonControl.Images = imageCollection1; // Create a ribbon page. RibbonPage page1 = new RibbonPage("Home"); // Create a ribbon page group. RibbonPageGroup group1 = new RibbonPageGroup("File"); // Create another ribbon page group. RibbonPageGroup group2 = new RibbonPageGroup("File 2"); // Create a button item using the CreateButton method. // The created item is automatically added to the item collection of the RibbonControl. BarButtonItem itemOpen = ribbonControl.Items.CreateButton("Open..."); itemOpen.ImageIndex = 7; itemOpen.ItemClick += new ItemClickEventHandler(itemOpen_ItemClick); // Create a button item using its constructor. // The constructor automatically adds the created item to the RibbonControl's item collection. BarButtonItem itemClose = new BarButtonItem(ribbonControl.Manager, "Close"); itemClose.ImageIndex = 12; itemClose.ItemClick += new ItemClickEventHandler(itemClose_ItemClick); // Create a button item using the default constructor. BarButtonItem itemPrint = new BarButtonItem(); // Manually add the created item to the item collection of the RibbonControl. ribbonControl.Items.Add(itemPrint); itemPrint.Caption = "Print"; itemPrint.ImageIndex = 9; itemPrint.ItemClick += new ItemClickEventHandler(itemPrint_ItemClick); // Add the created items to the group using the AddRange method. // This method will create bar item links for the items and then add the links to the group. group1.ItemLinks.AddRange(new BarItem[] { itemOpen, itemClose, itemPrint}); // Add the Open bar item to the second group. group2.ItemLinks.Add(itemOpen); // Add the created groups to the page. page1.Groups.Add(group1); page1.Groups.Add(group2); // Add the page to the RibbonControl. ribbonControl.Pages.Add(page1); //... void itemPrint_ItemClick(object sender, ItemClickEventArgs e) { //... } void itemClose_ItemClick(object sender, ItemClickEventArgs e) { //... } void itemOpen_ItemClick(object sender, ItemClickEventArgs e) { //... } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraBars.Ribbon Imports DevExpress.XtraBars ' Create a RibbonControl Dim ribbonControl As New RibbonControl() Me.Controls.Add(ribbonControl) ' Assign the image collection that will provide images for bar items. ribbonControl.Images = imageCollection1 ' Create a ribbon page. Dim page1 As New RibbonPage("Home") ' Create a ribbon page group. Dim group1 As New RibbonPageGroup("File") ' Create another ribbon page group. Dim group2 As New RibbonPageGroup("File 2") ' Create a button item using the CreateButton method. ' The created item is automatically added to the item collection of the RibbonControl. Dim itemOpen As BarButtonItem = ribbonControl.Items.CreateButton("Open...") itemOpen.ImageIndex = 7 AddHandler itemOpen.ItemClick, AddressOf itemOpen_ItemClick ' Create a button item using its constructor. ' The constructor automatically adds the created item to the RibbonControl's item collection. Dim itemClose As New BarButtonItem(ribbonControl.Manager, "Close") itemClose.ImageIndex = 12 AddHandler itemClose.ItemClick, AddressOf itemClose_ItemClick ' Create a button item using the default constructor. Dim itemPrint As New BarButtonItem() ' Manually add the created item to the item collection of the RibbonControl. ribbonControl.Items.Add(itemPrint) itemPrint.Caption = "Print" itemPrint.ImageIndex = 9 AddHandler itemPrint.ItemClick, AddressOf itemPrint_ItemClick ' Add the created items to the group using the AddRange method. ' This method will create bar item links for the items and then add the links to the group. group1.ItemLinks.AddRange(New BarItem() {itemOpen, itemClose, itemPrint}) ' Add the Open bar item to the second group. group2.ItemLinks.Add(itemOpen) ' Add the created groups to the page. page1.Groups.Add(group1) page1.Groups.Add(group2) ' Add the page to the RibbonControl. ribbonControl.Pages.Add(page1) '... Sub itemPrint_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs) '... End Sub Sub itemClose_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs) '... End Sub Sub itemOpen_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs) '... End Sub |