下面的示例展示了如何接管 BarManager.ShowToolbarsContextMenu 事件,来操作自定义菜单。 在本例中,把一个呈现超链接编辑器的新 bar item link 添加到该菜单中。 编辑器显示了“www.devexpress.com”文本,单击此文本将打开一个转到指定地址的浏览器窗口。

提供编辑工具的 bar item 是一个 BarEditItem。 为了把指定的编辑器与该 bar item 相关联,我们创建了一个相应的包含了编辑类型信息和编辑设置的 repository 项对象,并把它添加到 BarManager 的 ComponentEditorContainer.RepositoryItems 集合中,然后把它指派到 BarEditItem.Edit 属性。 该 bar item 被创建,并且在窗体的 Load 事件处理程序中被定制。

下面的插图展示了有新建项的自定义菜单:

C#CopyCode image复制代码
using XtraEditors.Repository;
private void frmMain_Load(object sender, System.EventArgs e) {
    //Create a new bar item representing a hyperlink editor
    BarEditItem item = new BarEditItem();
    //Create and customize a repository item representing a hyperlink editor
    RepositoryItemHyperLinkEdit ri = new RepositoryItemHyperLinkEdit();
    ri.SingleClick = true;
    //Add the repository item to the internal repository
    barManager1.RepositoryItems.Add(ri);
    //Assign the repository item to the bar item
    item.Edit = ri;
    //Provide the initial value for the editor
    item.EditValue = "www.devexpress.com";
    //Name the bar item
    item.Name = "MyHyperlinkItem";
    //Add the bar item to the bar manager
    barManager1.Items.Add(item);
}

private void barManager1_ShowToolbarsContextMenu(object sender, 
  ShowToolbarsContextMenuEventArgs e) {
    //Get the bar item by its name and create a link to it in the customization menu
    BarItemLink link = e.ItemLinks.Add(barManager1.Items["MyHyperlinkItem"]);
    //Customize the link
    link.Width = 120;
    link.BeginGroup = true;
}
Visual BasicCopyCode image复制代码
Imports XtraEditors.Repository

Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
 Handles MyBase.Load
    'Create a new bar item representing a hyperlink editor
    Dim item As BarEditItem = New BarEditItem
    'Create and customize a repository item representing a hyperlink editor
    Dim ri As RepositoryItemHyperLinkEdit = New RepositoryItemHyperLinkEdit
    ri.SingleClick = True
    'Add the repository item to the internal repository
    barManager1.RepositoryItems.Add(ri)
    'Assign the repository item to the bar item
    item.Edit = ri
    'Provide the initial value for the editor
    item.EditValue = "www.devexpress.com"
    'Name the bar item 
    item.Name = "MyHyperlinkItem"
    'Add the bar item to the bar manager
    barManager1.Items.Add(item)
End Sub

Private Sub barManager1_ShowToolbarsContextMenu(ByVal sender As Object, _
 ByVal e As DevExpress.XtraBars.ShowToolbarsContextMenuEventArgs) _
 Handles barManager1.ShowToolbarsContextMenu
    'Get the bar item by its name and create a link to it in the customization menu
    Dim link As BarItemLink = e.ItemLinks.Add(barManager1.Items("MyHyperlinkItem"))
    'Customize the link
    link.Width = 120
    link.BeginGroup = True
End Sub