说明
- 前台绘制全部菜单,根据角色展示菜单。数据库不让维护菜单之间的关系。为什么?我不知道!
- 本文用BarManager,BarSubItem,BarButtonItem,BarButtonItem
- 根据角色展示菜单
- menu 表 里只有两个字段 menu_name ;button_name;角色菜单表里只有 role_id;menu_name
- 整体逻辑就是: 所有按钮页面控件绘制(不是动态生成),根据角色菜单表关联菜单表找到没权限菜单名称隐藏菜单
- 不要说方法蠢。因为我也这么认为。但不让维护菜单关系,不让动态绘制,一切就这样吧。简单记录下
- 三级菜单样式 如下 :(BC 去哪里了?往下看)
- BarSubItem 1
- BarButtonItem A
- BarButtonItem D
- BarButtonItem E
- BarSubItem 2
- BarButtonItem X
- BarButtonItem Y
1. BarSubItem 隐藏
barSubItemNew.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
2. BarButtonItem 隐藏
barButtonItemNew.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
根据 字符串名称 ,创建控件(反射)
string btnName = menuButton.ButtonName;
object obj = this.GetType().GetField(btnName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(this);
Type type = obj.GetType();
if (type.Equals(typeof(DevExpress.XtraBars.BarSubItem)))
{
DevExpress.XtraBars.BarSubItem barSubItemNew = (DevExpress.XtraBars.BarSubItem)obj;
barSubItemNew.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
}
3. 根据 控件类型 设置控件属性(反射)
这方法目的是子菜单全部全部无权限,上级菜单也不展示
实际菜单是:
- BarSubItem 1
- BarButtonItem A
- BarButtonItem B .Visible false
- BarButtonItem C.Visible false
- BarButtonItem D
- BarButtonItem E
- BarSubItem 2
- BarButtonItem X
- BarButtonItem Y
别问我 为啥 B,C .Visible == false。。。合作开发我真没办法控制别人。。。但是会存在这种情况! 那么问题来了,怎么判断 我的子菜单全部隐藏? 就是 Visible ==ture 个数 = BarItemVisibility.Never 个数
Type FormType = this.GetType();
FieldInfo[] fi = FormType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
for(int times = 2; times >= 0; times--)
{
foreach (FieldInfo info in fi)
{
if (info.FieldType == typeof(DevExpress.XtraBars.BarSubItem))
{
int countNever = 0;
int countItermVisible = 0;
DevExpress.XtraBars.BarSubItem items = (info.GetValue(this)) as DevExpress.XtraBars.BarSubItem;
DevExpress.XtraBars.LinksInfo linksInfo = (items as DevExpress.XtraBars.BarSubItem).LinksPersistInfo;
for (int i = 0; i < linksInfo.Count; i++)
{
if (linksInfo[i].Visible == true)
{
countItermVisible++;
if (linksInfo[i].Item.Visibility == DevExpress.XtraBars.BarItemVisibility.Never)
{
countNever++;
}
}
}
if (countNever >= countItermVisible)
{
items.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
}
}
}
}
转载请说明出处。
|