DevExpress 中根据数据库字典动态生成卡式菜单
最近在写HR系统用到的第三方的Devexpress套件因为要使用权限机制控制不同用户进入系统显示菜单所以要配合字典数据动态生成。在WEB中这种问题灰常的轻松在winform里就稍微有点不同为了用DEV实现卡式菜单有组的概念具体如图红色部分
提示:这里用了菜单头、菜单组、菜单项来逐级生成 ,首先是菜单头便利创建然后判断是否有菜单组如果有菜单组就先生成菜单组然后把菜单项加入菜单组最后为按钮注册事件,这里的事情用一个公用方法搞定即可
/// <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); } }