3

我正在尝试找到如何将项目添加到 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;
4

2 回答 2

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);
}

逐步参考以包括弹出窗口:

如何:创建弹出菜单
如何:将项目添加到容器栏项目(菜单)填充
弹出菜单
BarManager.SetPopupContextMenu 方法

于 2012-04-16T14:50:53.740 回答
1

你的经理是空的:

manager = new BarManager();

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

从他们的BarManager 帮助页面

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

于 2012-04-16T14:24:00.393 回答