barManager.Menu(barSubItem)

时间:2021-09-19 12:01:00

DevExpress 的帮助文档是在太缺乏了,他自己的帮助只有简单描述,没有样例,而它的网站上的在线帮助只有利用它的设计器设计的ToolBar、PopupMenu ,没有利用代码开发的。
虽然要利用代码开发,可以参照它设计器背后的代码,但还是有些区别的。我最近就碰见了问题:
我通过反射初始化BarButtonItem,BarSubItem按钮实例,最后在主程序把这些按钮加到ToolBar上:
在用反射实例化BarSubItem时,要用该方法:
                        BarSubItem newItem = new BarSubItem();
                        newItem.Name = intMenuItemCount.ToString();
                        newItem.Id = intMenuItemCount++;
 newMenu.LinksPersistInfo.Add(new DevExpress.XtraBars.LinkPersistInfo((BarItem)item, beginGroup));
这里要注意:
1、Id必须唯一。
2、不要使用newMenu.AddItem((BarItem)item),这个方法会引起很多问题,如:beginGroup菜单分组无法实现, 我试图通过以下方法实现,但还是徒劳,而且有些编辑菜单不能使用该方法,如:BarToolbarsListItem工具栏列表菜单                   
                         BarItemLink itemLink = newMenu.AddItem((BarItem)item);
                          itemLink.BeginGroup = beginGroup;
3、在将实例化的BarSubItem加到ToolBar以后,还要递归把BarSubItem以及它的子菜单加入BarManager。
 private void AddMenuItem(DevExpress.XtraBars.BarItem menuItem)
        {
            if (menuItem != null)
            {
                //如果是编辑框工具条时使用
                if (menuItem is BarEditItem)
                {
                    this.BarMenuManager.RepositoryItems.Add((menuItem as BarEditItem).Edit);
                }
                this.BarMenuManager.Items.Add(menuItem);
                if (menuItem is DevExpress.XtraBars.BarSubItem)
                {
                    DevExpress.XtraBars.LinksInfo linksInfo = (menuItem as DevExpress.XtraBars.BarSubItem).LinksPersistInfo;
                    for (int i = 0; i < linksInfo.Count; i++)
                    {
                        AddMenuItem((BarItem)linksInfo[i].Item);
                    }
                }
            }
4、如果右键菜单也是通过反射实现的,那么在加载右键菜单时需要注意:
 public PopupMenu CreateContextMenu
{
             PopupMenu contextMenu = new PopupMenu();
                contextMenu.Popup += new EventHandler(ContextMenuPopupHandler);
            foreach (object item in buildItems)
                {
                    if (item is BarItem)
                    {
                        bool beginGroup = false;
                        beginGroup = (bool)(item as BarItem).Tag;

//contextMenu.LinksPersistInfo.AddRange(new LinkPersistInfo[] { new LinkPersistInfo((item as             BarItem),         beginGroup) });
                        BarItem barItem = item as BarItem;
                      
                       
                        AddMenuItem(barManager, barItem);
                        //用LinksPersistInfo右键菜单不出来
                        //contextMenu.LinksPersistInfo.Add(new LinkPersistInfo(barItem, beginGroup));
                        BarItemLink itemLink = contextMenu.AddItem(barItem);
                        itemLink.BeginGroup = beginGroup;
                    }
                }
                contextMenu.Manager = barManager; 
            return contextMenu;
}
//在此方法中注意 BarItemLink itemLink = (menuItem as BarSubItem).AddItem(item);此方法看似又多加了菜单,但实际不然,如果不用该方法,右键菜单中的菜单有子菜单时会无法显示。我就是让这个问题耗了我很多事间

private void AddMenuItem(DevExpress.XtraBars.BarManager barManager,DevExpress.XtraBars.BarItem menuItem)
        {
            if (menuItem != null)
            {              
                barManager.Items.Add(menuItem);
                if (menuItem is DevExpress.XtraBars.BarSubItem)
                {
                    DevExpress.XtraBars.LinksInfo linksInfo = (menuItem as DevExpress.XtraBars.BarSubItem).LinksPersistInfo;
                    int count = linksInfo.Count;
                    for (int i = 0; i < count; i++)
                    {
                        BarItem item = (BarItem)linksInfo[i].Item;
                        item.Manager = barManager;

BarItemLink itemLink = (menuItem as BarSubItem).AddItem(item);
                        itemLink.BeginGroup = linksInfo[i].BeginGroup;
                        AddMenuItem(barManager, item);
                    }
                }
            }
        }