向PopupMenu添加项目

时间:2012-04-16 13:32:10

标签: c# winforms devexpress

我试图找到如何向devExpress PopupMenu添加项目。我尝试过以下方法:

        manager = new BarManager();
        listBoxMenu = new PopupMenu(manager);

        listBoxMenu.ItemLinks.Add(manager.Items["Remove item"]);
        listBoxMenu.ItemLinks.Add(manager.Items["Clear items"]);

如此处所示http://documentation.devexpress.com/#WindowsForms/CustomDocument5472(位于底部),但它给出了一个错误,指出该项目未初始化。

添加商品的正确方法是什么?我无法在任何地方找到它。

编辑,我就是这样做的:

        //Creates the popup menu to be used for the keywords listbox
        manager = new BarManager();
        listBoxMenu = new PopupMenu(manager);

        item = new BarButtonItem(manager, "Copy");
        item2 = new BarButtonItem(manager, "Clear Item");
        item3 = new BarButtonItem(manager, "Clear All Items");

        listBoxMenu.ItemLinks.Add(item);
        listBoxMenu.ItemLinks.Add(item2);
        listBoxMenu.ItemLinks.Add(item3);

        //Adds the seperator on the second item
        item2.Links[0].BeginGroup = true;

        manager.ItemClick += manager_ItemClick;

2 个答案:

答案 0 :(得分:6)

检查此代码段并使用相同的方式实现。

//创建弹出窗口并管理对象

private DevExpress.XtraBars.BarManager barManager1;
        private DevExpress.XtraBars.PopupMenu buttonContextMenu;
        DevExpress.XtraBars.BarButtonItem menuButtonExport = new DevExpress.XtraBars.BarButtonItem();
        DevExpress.XtraBars.BarButtonItem menuButtonSave = new DevExpress.XtraBars.BarButtonItem();

        public TestForm8()
        {
            InitializeComponent();
            barManager1 = new BarManager();
            this.barManager1.Form = this;
            buttonContextMenu = new DevExpress.XtraBars.PopupMenu(barManager1);                 
            this.buttonContextMenu.Name = "subViewContextMenu";            


            menuButtonExport.Caption = "E&xport";
            menuButtonExport.Id = 1;
            menuButtonExport.Name = "menuButtonExport";
            menuButtonExport.ItemClick += new ItemClickEventHandler(menuButtonExport_ItemClick);

            menuButtonSave.Caption = "S&ave";
            menuButtonSave.Id = 2;
            menuButtonSave.Name = "menuButtonSave";
            menuButtonSave.ItemClick += new ItemClickEventHandler(menuButtonSave_ItemClick);
//add items to barmanager
            this.barManager1.Items.AddRange(new DevExpress.XtraBars.BarItem[] {
            menuButtonExport,
            menuButtonSave
            });
//create links between bar items and popup
            buttonContextMenu.ItemLinks.Add(barManager1.Items["menuButtonExport"]);
            buttonContextMenu.ItemLinks.Add(barManager1.Items["menuButtonSave"]);
//finally set the context menu to the control or use the showpopup method on right click of control
            barManager1.SetPopupContextMenu(btnInsert, buttonContextMenu);
}

逐步包含弹出窗口:

How to: Create a popup menu
How to: Add items to a container bar item (menu)
Populating Popup Menus
BarManager.SetPopupContextMenu Method

答案 1 :(得分:1)

你的经理是空的:

manager = new BarManager();

您链接到的示例是使用已创建的BarManagerbarManager1,我假设它是在设计器中创建的并填充了项目。

来自BarManager help page

  

将BarManager添加到表单/用户控件后,您可以使用栏管理器的自定义窗口或其Designer在表单上使用上下文菜单创建栏和栏命令。请参阅工具栏自定义部分,了解更多信息。