Devexpress Ribbon 动态生成菜单
Posted guojingmail2009
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Devexpress Ribbon 动态生成菜单相关的知识,希望对你有一定的参考价值。
/// <summary> /// 动态加载菜单 /// </summary> private void GetMenuBind() { //根据登录用户角色菜单动态创建 //循环创建卡菜单 DataTable baseModuleDT = BLL.BaseModule.GetDataTable("LevelNo=1", ""); for (int i = 0; i < baseModuleDT.Rows.Count; i++) { //创建卡 RibbonPage ribbonPage = new DevExpress.XtraBars.Ribbon.RibbonPage();//菜单卡对象定义 ribbonPage.Text = baseModuleDT.Rows[i]["FullName"].ToString(); this.ribbon.Pages.Add(ribbonPage); //获取组菜单 DataTable baseModuleDTG = BLL.BaseModule.GetDataTable("ParentId=" + baseModuleDT.Rows[i]["Id"].ToString() + " and LevelNo=2", ""); if (baseModuleDTG.Rows.Count <= 0) { //没有组 RibbonPageGroup ribbonPageGroup = new DevExpress.XtraBars.Ribbon.RibbonPageGroup();//菜单卡分组定义 ribbonPageGroup.Text = baseModuleDT.Rows[i]["FullName"].ToString();//卡片组名 ribbonPage.Groups.Add(ribbonPageGroup); DataTable baseModuleDTB = BLL.BaseModule.GetDataTable("ParentId=" + baseModuleDT.Rows[i]["Id"].ToString() + " and LevelNo=3", ""); for (int b = 0; b < baseModuleDTB.Rows.Count; b++) { Image image = Image.FromFile(filePath + baseModuleDTB.Rows[b]["MenuIco"].ToString());//获取文件 //功能按钮 BarButtonItem barButtonItem = new DevExpress.XtraBars.BarButtonItem(); barButtonItem.Caption = baseModuleDTB.Rows[b]["FullName"].ToString(); barButtonItem.LargeGlyph = image;// global::MemberManager.Properties.Resources.group_key; barButtonItem.Name = baseModuleDTB.Rows[b]["WinFrom"].ToString(); barButtonItem.Tag = baseModuleDTB.Rows[b]["WinFrom"].ToString().Substring(0, baseModuleDTB.Rows[b]["WinFrom"].ToString().IndexOf(‘.‘));//截取文件名点以前的 ribbonPageGroup.ItemLinks.Add(barButtonItem); barButtonItem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.barButtonItem_ItemClick);//注册事件 } } else { for (int g = 0; g < baseModuleDTG.Rows.Count; g++) { //创建组 RibbonPageGroup ribbonPageGroup = new DevExpress.XtraBars.Ribbon.RibbonPageGroup();//菜单卡分组定义 ribbonPageGroup.Text = baseModuleDTG.Rows[g]["FullName"].ToString();//卡片组名 ribbonPage.Groups.Add(ribbonPageGroup); DataTable baseModuleDTB = BLL.BaseModule.GetDataTable("ParentId=" + baseModuleDTG.Rows[g]["Id"].ToString() + " and LevelNo=3", ""); for (int b = 0; b < baseModuleDTB.Rows.Count; b++) { Image image = Image.FromFile(filePath + baseModuleDTB.Rows[b]["MenuIco"].ToString());//获取文件 //功能按钮 BarButtonItem barButtonItem = new DevExpress.XtraBars.BarButtonItem(); barButtonItem.Caption = baseModuleDTB.Rows[b]["FullName"].ToString(); barButtonItem.LargeGlyph = image;// global::MemberManager.Properties.Resources.group_key; barButtonItem.Name = baseModuleDTB.Rows[b]["WinFrom"].ToString(); //barButtonItem.Tag = baseModuleDTB.Rows[b]["WinFrom"].ToString().Substring(0, baseModuleDTB.Rows[b]["WinFrom"].ToString().IndexOf(‘.‘));//截取文件名点以前的 ribbonPageGroup.ItemLinks.Add(barButtonItem); barButtonItem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.barButtonItem_ItemClick);//注册事件 } } } } } /// <summary> /// 动态调出窗体事件 注:动态调用窗体名需和数据库中名称完全一致 /// </summary> private void barButtonItem_ItemClick(object sender, ItemClickEventArgs e) { if (!ShowChildrenForm(e.Item.Tag.ToString())) { if (this.MdiChildren.Length < childFormMaxNumber) { DictItemUtil.AddLog("成功打开" + e.Item.Caption.ToString()); Assembly asm = Assembly.Load("MemberManager");//程序集名 object frmObj = asm.CreateInstance("MemberManager." + e.Item.Tag.ToString());//程序集+form的类名。 Form childForm = (Form)frmObj; childForm.Tag = e.Item.Tag.ToString(); //tag属性要重新写一次,否则在第二次的时候取不到。原因还不清楚。有知道的望告知。 childForm.MdiParent = this; childForm.Show(); } else MessageUtil.ShowWarning(message); } }
以上是关于Devexpress Ribbon 动态生成菜单的主要内容,如果未能解决你的问题,请参考以下文章
DevExpress WinForms使用教程:Ribbon性能
DevExpress 隐藏Ribbon中barbuttonItem的SuperTip